【uniapp】小程序端实现分包异步化

背景

微信小程序、支付宝小程序、抖音小程序等支持 占位组件,但是 uniapp 只支持在 pages.json 下的页面中配置 componentPlaceholder, 并不支持在某个具体的组件中添加此配置,于是我写了 github.com/chouchouji/... 解决此问题。

特性

  • 配置简单,开箱即用
  • 支持 vue、nvue、uvue 三种文件
  • 支持 uni-app 和 uni-app-x 项目
  • 支持 组合式 和 选项式 两种写法

使用指南

安装

shell 复制代码
// npm
npm i @binbinji/vite-plugin-component-placeholder -D

// yarn
yarn add @binbinji/vite-plugin-component-placeholder -D

// pnpm
pnpm add @binbinji/vite-plugin-component-placeholder -D

引入插件

javascript 复制代码
// vite.config.*
import componentPlaceholderPlugin from '@binbinji/vite-plugin-component-placeholder'
import uni from '@dcloudio/vite-plugin-uni'
import { defineConfig } from 'vite'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [uni(), componentPlaceholderPlugin()],
})

在 vue/nuve/uvue 文件中添加配置

vue 相关文件中添加 componentPlaceholder 配置

组合式文件
vue 复制代码
<template>
  <view>test page</view>
  <test></test>
</template>

<script setup>
import test from './test.vue'

defineOptions({
  componentPlaceholder: {
    test: 'view',
  },
})
</script>
选项式文件
vue 复制代码
<template>
  <view>test page</view>
  <test></test>
</template>

<script>
import test from './test.vue'

export default {
  componentPlaceholder: {
    test: 'view',
  },
}
</script>
相关推荐
NiceCloud喜云3 小时前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
wordbaby3 小时前
React Native + RNOH:跨页面数据回传的最佳实践与避坑指南
前端·react native
丷丩3 小时前
MapLibre GL JS第22课:查看本地GeoJSON
前端·javascript·map·mapbox·maplibre gl js
Front思4 小时前
AI前端工程师需要具备能力+
前端·人工智能·ai
kyh10033811205 小时前
Cocos Creator 《打螺丝消除游戏》源码+实现
游戏·微信小程序·小程序·打螺丝小游戏源码·微笑小游戏源码
ZC跨境爬虫6 小时前
跟着 MDN 学CSS day_29:(掌握文本与字体样式的核心艺术)
前端·css·ui·html·tensorflow
李子琪。7 小时前
网络空间安全深度实战:CSRF 漏洞原理剖析与基于 Token 的纵深防御体系构建(全栈实验报告)
前端·安全·csrf
冰暮流星7 小时前
javascript之history对象介绍
前端·笔记
IT_陈寒8 小时前
Vite热更新失灵?你可能漏了这个配置
前端·人工智能·后端
丷丩8 小时前
MapLibre GL JS第19课:实时更新要素
前端·javascript·gis·map·mapbox·maplibre gl js