DCloud_UNI_JBB
DCloud_UNI_JBB
  • 发布:2025-09-25 19:37
  • 更新:2025-12-11 11:03
  • 阅读:1646

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

分类:uni-app

背景

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

可以通过这个 插件 https://github.com/uni-toolkit/uni-toolkit 来解决此问题

特性

  • 配置简单,开箱即用
  • 支持 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({  
  // #ifdef MP  
  componentPlaceholder: {  
    test: 'view',  
  }  
  // #endif  
})  
</script>

选项式文件

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

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

export default {  
  components: {  
    test,  
  },  
  // #ifdef MP  
  componentPlaceholder: {  
    test: 'view',  
  }  
  // #endif  
}  
</script>
5 关注 分享
7***@qq.com julytian DCloud_UNI_OttoJi zzdev twentyLee

要回复文章请先登录注册

DCloud_UNI_JBB

DCloud_UNI_JBB (作者)

回复 twentyLee :
15 和 16 版本为了兼容 node 16 ,做了一点代码修改,最好是能给我一个简单的可复现demo,我排查一下
2025-12-11 11:03
twentyLee

twentyLee

回复 twentyLee :
刚刚试了一下 14版本可以正常使用,15、16 版本会出现这个问题
2025-12-11 10:56
DCloud_UNI_JBB

DCloud_UNI_JBB (作者)

回复 twentyLee :
im里面私聊发我一个demo
2025-12-11 10:54
twentyLee

twentyLee

大佬今天重启一下 编译报错了
10:40:43.646 ERROR Error loading vue.config.js:
10:40:43.646 ERROR Error: Cannot find module '@dcloudio/uni-cli-i18n'

10:42:07.993 ERROR Error loading vue.config.js:
10:42:07.993 ERROR Error: Cannot find module 'semver'

安装 这些插件后 还是无法正常解析,这是什么原因啊

10:42:52.112 ​Failed to parse component-config in D:\work\vue\invest_consult_platform_vue\pages\toolkit\diagnosis\details.vue:​ ​{}​
10:42:52.159 ​Failed to parse component-config in D:\work\vue\invest_consult_platform_vue\pages\toolkit\optional\report.vue:​ ​{}​
10:42:52.159 ​Failed to parse component-config in D:\work\vue\invest_consult_platform_vue\pages\toolkit\dats\details.vue:​ ​{}​
10:42:52.159 ​Failed to parse component-config in D:\work\vue\invest_consult_platform_vue\pages\toolkit\dcf\dcf.vue:​ ​{}​
10:42:52.159 ​Failed to parse component-config in D:\work\vue\invest_consult_platform_vue\pages\toolkit\dats\alldats.vue:​ ​{}​
10:42:52.159 ​Failed to parse component-config in D:\work\vue\invest_consult_platform_vue\pages\toolkit\dats\businessdept.vue:​ ​{}​
10:42:52.159 ​Failed to parse component-config in D:\work\vue\invest_consult_platform_vue\pages\simulate\simulatedetail.vue:​ ​{}​
10:42:52.159 ​Failed to parse component-config in D:\work\vue\invest_consult_platform_vue\pages\simulate\simulate.vue:​ ​{}​
2025-12-11 10:43
DCloud_UNI_JBB

DCloud_UNI_JBB (作者)

回复 9***@tmp.dcloud.io :
发了,最新版本要求node16+
2025-12-09 10:35
9***@tmp.dcloud.io

9***@tmp.dcloud.io

回复 z***@126.com :
发布到最新版本了嘛
2025-12-09 10:20
DCloud_UNI_JBB

DCloud_UNI_JBB (作者)

回复 1***@qq.com :
可以,私聊你了
2025-12-01 15:17
1***@qq.com

1***@qq.com

webpack 版本运行到抖音端, 进入使用异步组件的页面 编辑器直接卡死,能帮忙看下吗
2025-12-01 15:04
DCloud_UNI_JBB

DCloud_UNI_JBB (作者)

回复 g***@163.com :
我看看能不能提供一个脚本给你
2025-11-24 11:15
DCloud_UNI_JBB

DCloud_UNI_JBB (作者)

回复 g***@163.com :
im中我私聊你
2025-11-24 11:13