uniapp common_vendor.index.share is not a function 如何解决

在uniapp开发中调用common_vendor.index.share方法时提示"common_vendor.index.share is not a function"错误,请问这是什么原因导致的?该如何正确调用分享功能?我的代码是直接按照官方文档写的:common_vendor.index.share({...}),但运行时报这个错误。需要引入什么特别的配置或插件吗?

2 回复

检查是否引入common_vendor模块。确保在main.js中正确导入并挂载到Vue原型上。若已引入,尝试重启HBuilderX或重新编译项目。


在UniApp中出现 common_vendor.index.share is not a function 错误,通常是因为 uni.share() 方法未正确引入或调用方式有误。以下是解决方案:

1. 检查API引入方式

确保在页面中正确引入UniApp的API(通常无需手动引入,但需检查环境):

// 无需单独引入uni对象,直接使用即可
uni.share({
  provider: 'weixin',
  scene: 'WXSceneSession',
  type: 0,
  title: '分享标题',
  summary: '分享描述',
  href: 'https://example.com',
  success: function (res) {
    console.log('分享成功:', res);
  },
  fail: function (err) {
    console.log('分享失败:', err);
  }
});

2. 验证运行环境

  • H5端:部分分享功能需配置JS-SDK(如微信分享)。
  • 小程序端:需在 manifest.json 中配置权限,并确保调用的API受支持。
  • App端:需引入原生插件(如uni-share)或使用5+ API。

3. 排查common_vendor问题

  • 若错误指向 common_vendor,尝试 重启HBuilderX 或重新编译项目。
  • 删除 unpackage 目录后重新运行项目,清除缓存。

4. 检查依赖配置

manifest.json 中确认已启用分享模块:

{
  "app-plus": {
    "modules": {
      "Share": {}
    }
  }
}

5. 替代方案

若问题持续,使用 uni.showActionSheet 配合平台API手动实现分享:

uni.showActionSheet({
  itemList: ['分享到微信', '分享到微博'],
  success: (res) => {
    if (res.tapIndex === 0) {
      // 调用微信分享逻辑
    }
  }
});

总结步骤:

  1. 检查代码调用方式是否正确。
  2. 清理项目缓存并重启开发工具。
  3. 确认平台兼容性及配置。
  4. 使用调试工具查看具体错误栈。

通常通过 重启开发工具 + 清理缓存 可解决大部分 common_vendor 相关错误。

回到顶部