DCloud_UNI_JBB
DCloud_UNI_JBB
  • 发布:2025-09-25 19:37
  • 更新:2025-12-01 15:17
  • 阅读:1166

微信、支付宝、抖音等小程序支持分包异步化---跨分包自定义组件引用

分类:uni-app

背景

微信小程序支持分包异步化 跨分包自定义组件引用,但是 uniapp 目前只支持在 pages.json 下的页面中配置 componentPlaceholder, 并不支持在某个具体的组件中添加此配置。

可以通过这个 插件 来解决此问题

特性

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

Vue3

安装

npm install @uni_toolkit/vite-plugin-component-config -D  
# 或  
pnpm add @uni_toolkit/vite-plugin-component-config -D  
# 或  
yarn add @uni_toolkit/vite-plugin-component-config -D

使用方法

配置 vite.config.js

// vite.config.js  
import { defineConfig } from 'vite'  
import uni from '@dcloudio/vite-plugin-uni'  
import componentConfig from '@uni_toolkit/vite-plugin-component-config'  

export default defineConfig({  
  plugins: [  
    componentConfig(), // 在 uni 插件之前调用  
    uni(),  
  ]  
})

修改 Vue 文件

<template>  
  <view class="container">  
    <text>Hello World</text>  
  </view>  
</template>  

<script>  
export default {  
  name: 'MyComponent'  
}  
</script>  

// #ifdef MP  
<component-config>  
// 此处必须是标准的 json 对象,支持条件编译  
{  
  "usingComponents": {  
    "custom-button": "/components/custom-button"  
  },  
  "styleIsolation": "apply-shared",  
  "componentPlaceholder": {    
    "test": "view",    
  }    
}  
</component-config>  
// #endif

Vue2

  • 0.0.15 以上的版本对于 node 最低版本要求是 16+

安装

npm install @uni_toolkit/webpack-plugin-component-config -D  
# 或  
pnpm add @uni_toolkit/webpack-plugin-component-config -D  
# 或  
yarn add @uni_toolkit/webpack-plugin-component-config -D

使用方法

配置 vue.config.js

const WebpackComponentConfigPlugin = require('@uni_toolkit/webpack-plugin-component-config').default;  

module.exports = {  
  configureWebpack: {  
    plugins: [  
      new WebpackComponentConfigPlugin()  
    ]  
  }  
};

修改 Vue 文件

<template>  
  <view class="container">  
    <text>Hello World</text>  
  </view>  
</template>  

<script>  
export default {  
  name: 'MyComponent'  
}  
</script>  

// #ifdef MP  
<component-config>  
// 此处必须是标准的 json 对象,支持条件编译  
{  
  "usingComponents": {  
    "custom-button": "/components/custom-button"  
  },  
  "styleIsolation": "apply-shared",  
  "componentPlaceholder": {    
    "test": "view",    
  }    
}  
</component-config>  
// #endif

Tips

如果你是 vue3 项目,并且单纯只想添加 componentPlaceholder 配置,可以使用 https://github.com/chouchouji/vite-plugin-component-placeholder

安装

// 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

使用

1. 引入插件 @binbinji/vite-plugin-component-placeholder

// 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()],  
})

2. 在 vue/nvue/uvue 文件中添加配置

vue 相关文件中添加 componentPlaceholder 配置

组合式文件

<template>  
  <view>test page</view>  
  <test></test>  
</template>  

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

defineOptions({  
  componentPlaceholder: {  
    test: 'view',  
  },  
})  
</script>

选项式文件

<template>  
  <view>test page</view>  
  <test></test>  
</template>  

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

export default {  
  components: {  
    test,  
  },  
  componentPlaceholder: {  
    test: 'view',  
  },  
}  
</script>
3 关注 分享
7***@qq.com julytian DCloud_UNI_OttoJi

要回复文章请先登录注册

z***@126.com

z***@126.com

unipp vue2 报错,怎么解决
ERROR Error [ERR_REQUIRE_ESM]: require() of ES Module E:\java\wxappLcapRihi\node_modules\lodash-es\lodash.js from E:\java\wxappLcapRihi\node_modules\@uni_toolkit\webpack-plugin-component-config\dist\index.cjs not supported.
Instead change the require of lodash.js in E:\java\wxappLcapRihi\node_modules\@uni_toolkit\webpack-plugin-component-config\dist\index.cjs to a dynamic import() which is available in all CommonJS modules.
2025-10-29 12:27
DCloud_UNI_JBB

DCloud_UNI_JBB (作者)

回复 1***@qq.com :
用这个 https://github.com/chouchouji/vite-plugin-component-placeholder
2025-10-28 10:37
1***@qq.com

1***@qq.com

请问现在还支持在<script setup>里使用defineOptions吗
2025-10-28 09:20
DCloud_UNI_JBB

DCloud_UNI_JBB (作者)

回复 7***@qq.com :
升级到 0.0.3 试试还有没有问题
2025-10-13 11:02
DCloud_UNI_JBB

DCloud_UNI_JBB (作者)

回复 7***@qq.com :
你可以先临时修改node-modules下面的那个包,找到dist下的index.cjs文件,搜索Object.assign,那里就是合并json的地方,可以修改一下默认覆盖这个策略。也欢迎到我的github仓库提个pr
2025-10-12 10:32
DCloud_UNI_JBB

DCloud_UNI_JBB (作者)

回复 7***@qq.com :
应该是bug,我今天修复一下
2025-10-12 10:19
7***@qq.com

7***@qq.com

回复 DCloud_UNI_JBB :
可以用,非常感谢。我发现usingComponents好像会把原来引用的组件覆盖,需要删除usingComponents或者在usingComponents中重新声明原来引用的组件,这是正常的吗?
2025-10-12 09:51
DCloud_UNI_JBB

DCloud_UNI_JBB (作者)

回复 7***@qq.com :
文章我更新了,你看vue2的部分
2025-10-11 19:20
DCloud_UNI_JBB

DCloud_UNI_JBB (作者)

回复 7***@qq.com :
https://github.com/uni-toolkit/uni-toolkit/tree/main/packages/webpack-plugin-component-config 试试这个
2025-10-11 19:15
7***@qq.com

7***@qq.com

回复 DCloud_UNI_JBB :
好的,谢谢你。
2025-10-11 16:52