English
This document is intended to introduce how to compile a uni-app project into an applet plugin. If you want to know how to reference and use applet plugins in uni-app, please refer to the document: Using applet plugins
The applet plug-in specification is defined by the applet manufacturer. A plug-in is a package of a set of js interfaces, custom components or pages, and is used to be embedded in the applet.
uni-app can not only develop a complete applet, but also compile it into a applet plug-in.
Platform Difference Description
微信小程序 | 支付宝小程序 | 百度小程序 | 抖音小程序、飞书小程序 | QQ 小程序 | 快手小程序 | 京东小程序 |
---|---|---|---|---|---|---|
√ | √ ( 3.2.9+ ) | x | x | x | x | x |
Notice
WeChat applet plugin
, the basic library version 1.9.6
began to support. (If the plugin includes pages, the base library version 2.1.0
is required.)Alipay applet plug-in
, the Alipay IDE version is required to be 0.60 and aboveCompile to WeChat applet plugin result
plugin
├── components // 插件提供的自定义组件(可以有多个)
│ ├── hello-component.js
│ ├── hello-component.json
│ ├── hello-component.wxml
│ └── hello-component.wxss
├── pages
│ ├── hello-page.js // 插件提供的页面(可以有多个)
│ ├── hello-page.json
│ ├── hello-page.wxml
│ └── hello-page.wxss
├── index.js // 插件的 js 接口
└── plugin.json // 插件配置文件
plugin.json is at the same level as pages.json in the uni-app project. All components, pages and js interfaces open to third-party applets must be declared in plugin.json
mp-weixin
{
"publicComponents": {
"hello-component": "components/hello-component"
},
"pages": {
"hello-page": "pages/hello-page"
},
"main": "index.js"
}
The meaning of each configuration is as follows:
publicComponents
: List all custom components open to the applet.pages
: List all pages open to the applet.main
: The plugin's js interface for third-party applets.mp-alipay
{
"publicComponents": {
"hello-component": "components/hello-component"
},
"publicPages": {
"hello-pages": "pages/hello-page"
},
"pages": [
"pages/hello-page",
"pages/index"
],
"main": "index.js"
}
The meaning of each configuration is as follows:
publicComponents
: List all custom components open to the applet.publicPages
: List all pages open to the applet.pages
: List all the pages of the plugin (including pages that are open to Mini Programs and those that are not open to Mini Programs).main
: The plugin's js interface for third-party applets.Notice
pages
item in mp-weixin
has the same function as the publicPages
item in mp-alipay
mp-alipay
for external use need to be declared in pages
, array typeplugin.json
to processyarn dev:mp-weixin -- --plugin plugin-name
.plugin-name
is the name of the compiled plugin package. The executable MiniApp plug-in code is in project root directory\dist\dev\mp-weixin\plugin-name
.mp-alipay
platform plugin will be compiled and released later, please pay attention to the update log.uni-app project
. You can configure relevant information in manifest.json
. [Details](https://uniapp.dcloud.io/component/mp-weixin-plugin?id= %e9%85%8d%e7%bd%ae%e5%b0%8f%e7%a8%8b%e5%ba%8f%e6%8f%92%e4%bb%b6)app.json
of the project:
Sometimes the project not only needs to be compiled into a plug-in, but also needs to be run as a normal applet, but some APIs are not applicable to both ends. In this case, you can use custom conditional compilation to distinguish them.
To customize conditional compilation (details), add the following configuration to package.json
:
"uni-app": {
"scripts": {
"mp-wx-plugin": {
"title": "微信小程序插件",
"env": {
"UNI_PLATFORM": "mp-weixin"
},
"define": {
"MP-WX-PLUGIN": true
}
},
"mp-ali-plugin": {
"title": "阿里小程序插件",
"env": {
"UNI_PLATFORM": "mp-alipay"
},
"define": {
"MP-ALI-PLUGIN": true
}
},
}
}
Use conditional compilation:
// #ifdef MP-WX-PLUGIN
/**
* CODE
*/
// #endif
Execute the command when compiling: yarn dev:custom mp-wx-plugin --plugin test-plugin
, you can write the script into script
, and each execution is more simplified.
{
"dev:mp-wx-plugin": "yarn dev:custom mp-wx-plugin --plugin",
"dev:mp-ali-plugin": "yarn dev:custom mp-ali-plugin --plugin"
}
Notice
import helloList from '.../helloList';
Vue.component('hello-list', helloList);
pages.json
.plugins
compiled by uni-app
that need to run in the same applet, do not have the same name.\
. This name will be used to mount a method.-
has been manually replaced with _
, do not write other special characters.api
. For details, please refer to the relevant description of the official applet documentation