uni-app命令行创建uni+vue3项目开发微信小程序插件编译后运行报无法找到插件
uni-app命令行创建uni+vue3项目开发微信小程序插件编译后运行报无法找到插件
操作步骤:
- 根据官方提供的文档,使用命令行创建vue3+uniapp小程序项目,开发小程序插件。
"scripts": {
...
"dev:mp-wx-plugin-official": "yarn dev:custom mp-wx-plugin --plugin test-plugin", // 由官方文档提供
"dev:mp-wx-plugin": "cross-env NODE_ENV=development UNI_OUTPUT_DIR=./dist/dev/plugin-app/plugin uni -p mp-wx-plugin --plugin plugin", // 指定目录生成plugin项目文件
"dev:mp-wx": "cross-env NODE_ENV=development UNI_PLATFORM=mp-weixin UNI_OUTPUT_DIR=./dist/dev/plugin-app/miniprogram uni -p mp-weixin" // 指定目录生成小程序项目文件
},
"uni-app": {
"scripts": {
...
"mp-wx-plugin": {
"title": "微信小程序插件",
"env": {
"UNI_PLATFORM": "mp-weixin"
},
"define": {
"MP-WX-PLUGIN": true
}
}
}
},
- 分别运行
npm run dev:mp-wx-plugin和npm run dev:mp-wx进行编译,编译后在dist/dev/plugin-app根目录中新增project.config.json和project.private.config.json。
// project.config.json
{
"miniprogramRoot": "miniprogram/",
"pluginRoot": "plugin/",
"compileType": "plugin",
"libVersion": "latest",
"srcMiniprogramRoot": "miniprogram/",
"appid": "wx62de7623ad634e12",
"setting": {
"es6": false,
"postcss": false,
"compileWorklet": false,
"minified": false,
"uglifyFileName": false,
"uploadWithSourceMap": true,
"enhance": false,
"packNpmManually": false,
"packNpmRelationList": [],
"minifyWXSS": true,
"minifyWXML": true,
"localPlugins": false,
"condition": false,
"swc": false,
"disableSWC": true,
"babelSetting": {
"ignore": [],
"disablePlugins": [],
"outputPath": ""
},
"disableUseStrict": false,
"useCompilerPlugins": false,
"ignoreUploadUnusedFiles": true
},
"simulatorPluginLibVersion": {},
"packOptions": {
"ignore": [],
"include": []
},
"editorSetting": {}
}
// project.private.config.json
{
"libVersion": "2.30.4",
"projectname": "miniprogram-1",
"setting": {
"urlCheck": true,
"coverView": false,
"lazyloadPlaceholderEnable": false,
"skylineRenderEnable": false,
"preloadBackgroundData": false,
"autoAudits": false,
"useApiHook": true,
"useApiHostProcess": true,
"showShadowRootInWxmlPanel": false,
"useStaticServer": false,
"useLanDebug": false,
"showES6CompileOption": false,
"compileHotReLoad": false,
"bigPackageSizeSupport": false,
"checkInvalidKey": true,
"ignoreDevUnusedFiles": true
}
}
- 打开微信开发者工具-导入项目(开发模式选择:插件)
预期结果:
开发者工具正常运行编译后的项目,无报错
实际结果:
开发者工具报错TypeError: wx$2.canIUse is not a function,且提示找不到插件
TypeError: wx$2.canIUse is not a function
at vendor.js:1665
at VM6866 WASubContext.js:1
at f.runWith (VM6866 WASubContext.js:1)
at I (VM6866 WASubContext.js:1)
at VM6866 WASubContext.js:1
at app.js:3
at VM6866 WASubContext.js:1
at f.runWith (VM6866 WASubContext.js:1)
at I (VM6866 WASubContext.js:1)
at VM6866 WASubContext.js:1(env: Windows,mp,1.06.2504030; lib: 2.30.4)
Component is not found in path "plugin://wx62de7623ad634e12/chanin-transfer" (using by "wxPages/index/index")(env: Windows,mp,1.06.2504030; lib: 2.30.4)
bug描述:
命令行创建uni+vue3项目开发微信小程序插件,编译后运行报无法找到插件,且微信开发者工具报错提示
TypeError: wx$2.canIUse is not a function
at vendor.js:1665
at VM6866 WASubContext.js:1
at f.runWith (VM6866 WASubContext.js:1)
at I (VM6866 WASubContext.js:1)
at VM6866 WASubContext.js:1
at app.js:3
at VM6866 WASubContext.js:1
at f.runWith (VM6866 WASubContext.js:1)
at I (VM6866 WASubContext.js:1)
at VM6866 WASubContext.js:1(env: Windows,mp,1.06.2504030; lib: 2.30.4)
附件plugin-app.rar为编译后的项目文件

更多关于uni-app命令行创建uni+vue3项目开发微信小程序插件编译后运行报无法找到插件的实战教程也可以访问 https://www.itying.com/category-93-b0.html
看起来编译产物没什么问题,测试一下原生微信小程序这样写是否有问题
更多关于uni-app命令行创建uni+vue3项目开发微信小程序插件编译后运行报无法找到插件的实战教程也可以访问 https://www.itying.com/category-93-b0.html
把plugin文件单独拎出来,放到微信开发者工具自己生成的插件项目中,根本运行不了,报错提示TypeError: wx$2.canIUse is not a function,页面显示空白
而且不能正常像微信开发插件那样跳转到插件页面
遇到同样问题
原生微信小程序插件是不支持 canIUse 方法的,打包产物中有该方法

不仅有该方法,而且打包的产物, 宿主小程序跳转的插件页面都说没有注册.
Page “plugin://plugin-app/index” has not been registered yet.
而且无法进行调试, 怀疑uniapp是不能正常编译为插件的, 不然官方文档怎么没有详细的说明呢?
编译步骤 将 CLI 创建的工程编译器依赖更新到 3.2.1 以上,更新编译器依赖参考:https://uniapp.dcloud.net.cn/quickstart-cli.html#cliversion。 执行命令行:yarn dev:mp-weixin – --plugin plugin-name。 其中 plugin-name 为编译出插件包的名字。 项目根目录\dist\dev\mp-weixin\plugin-name 中即是可执行小程序插件代码。 mp-alipay 平台插件编译后续发布,请留意更新日志。
是对应要改这个版本? CLI版本号: 3.0.0-4070620250821001
可是获取不到 3.2.1 以上的依赖…
这个问题通常是由于微信开发者工具的基础库版本与uni-app编译后的代码不兼容导致的。wx$2.canIUse is not a function错误表明微信API调用方式在特定基础库版本下存在兼容性问题。
检查以下几个方面:
-
基础库版本兼容性:在微信开发者工具中,将基础库版本设置为最新稳定版。在项目设置中调整基础库版本,避免使用过旧或过新的版本。
-
project.config.json配置:确保
libVersion字段设置为"latest"或具体的兼容版本号。同时检查srcMiniprogramRoot和pluginRoot路径配置是否正确指向编译输出目录。 -
插件声明配置:在miniprogram目录的app.json中需要正确声明插件:
"plugins": {
"myPlugin": {
"version": "1.0.0",
"provider": "wx62de7623ad634e12"
}
}

