官方内容:
onShareAppMessage(OBJECT)
小程序中用户点击分享后,在 js 中定义 onShareAppMessage 处理函数(和 onLoad 等生命周期函数同级),设置该页面的分享信息。
- 用户点击分享按钮的时候会调用。这个分享按钮可能是小程序右上角原生菜单自带的分享按钮,也可能是开发者在页面中放置的分享按钮(
<button open-type="share">
); - 分享 | uni-app官网
问题是在UNIAPP VUE3 setup语法糖用法里,
onShareAppMessage不触发,
解决方法也很简单,
页面里除了setup语法糖外,再写一个<script>里面用选项式API的写法就可以触发了.
//在template中写按钮 <button open-type="share">share</button>
<script setup>
//...
import {onShareAppMessage} from '@dcloudio/uni-app' //这句不可少
//...
</script>
//下面是选项式写法,和上面同时存在,实测可以触发
<script>
export default {
onShareAppMessage(res) {
if (res.from === 'button') {// 来自页面内分享按钮
console.log(res.target)
}
return {
//返回的对象实现自定义,具体看文档
title: '自定义分享标题3333',
path: '/pages/test/test?id=123'
}
}
}
</script>