uni-app onShareAppMessage Vue3 Setup模式下无效

uni-app onShareAppMessage Vue3 Setup模式下无效

操作步骤:

onShareAppMessage(e => {  
    console.log(e)  
})  

onShareTimeline(e => {  
    console.log(e)  
})

预期结果:

打印出e的值


# 实际结果:

无任何输出

bug描述:

vue3 setup模式下不调用onShareAppMessage


| 信息类别         | 详细信息          |
|------------------|-------------------|
| 产品分类         | uniapp/小程序/微信 |
| PC开发环境操作系统 | Mac               |
| PC开发环境操作系统版本号 | 12.0.1            |
| HBuilderX类型    | Alpha             |
| HBuilderX版本号  | 3.3.2             |
| 第三方开发者工具版本号 | 1.05.2112141      |
| 基础库版本号     | 2.21.0            |
| 项目创建方式     | HBuilderX         |

![Image](https://www.itying.com/uniimg.php?url=https://img-cdn-tc.dcloud.net.cn/uploads/questions/20211226/df16f0b399b51de35ad5c2829ad954b0.jpg)
![Image](https://www.itying.com/uniimg.php?url=https://img-cdn-tc.dcloud.net.cn/uploads/questions/20211226/66821075e39be3e4c387f6b8a789e3f1.jpg)

更多关于uni-app onShareAppMessage Vue3 Setup模式下无效的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

稍后会优化(比如扫描当前页面是否使用了onShareAppMessage,但组件内定义的无法扫描确认),但无法解决各种使用情况,详情参考: https://github.com/dcloudio/uni-app/issues/3097

更多关于uni-app onShareAppMessage Vue3 Setup模式下无效的实战教程也可以访问 https://www.itying.com/category-93-b0.html


在 Vue3 的 setup 语法糖中,onShareAppMessageonShareTimeline 需要从 uni-app 的特定 API 中导入,并使用 define 前缀来定义。直接调用 onShareAppMessage 不会生效,因为它不是 Vue3 的组合式 API。

正确的写法如下:

import { onShareAppMessage, onShareTimeline } from '[@dcloudio](/user/dcloudio)/uni-app'

onShareAppMessage(() => {
  console.log('分享到好友')
  return {
    title: '分享标题',
    path: '/pages/index/index'
  }
})

onShareTimeline(() => {
  console.log('分享到朋友圈')
  return {
    title: '朋友圈分享标题'
  }
})
回到顶部