uniapp 微信小程序端分享到微信朋友圈 (坑)
- 首先必须要有这两个函数 onShareAppMessage onShareTimeline 这两个函数跟data平级
- 然后是这两个函数的内容如下 参数res需要传,即使不用
onShareAppMessage(res) {
return {
title: this.title,
path: this.path,
imageUrl: this.dispose.shareImg
};
},
// 自定义页面的分享到朋友圈
onShareTimeline(res) {
return {
title: this.title,
query: this.query,
imageUrl: this.dispose.shareImg,
}
},
- 不要在函数里面请求接口,或者写逻辑,因为return会先走,在里面写逻辑很大概率会导致,自定义title,自定义图片失效。
- onShareTimeline 分享朋友圈的属性是query不需要path 文档如下
- 以上只针对分享到朋友圈
注:如果你的项目中使用了 uni-simple-router 这个插件的话,在获取参数的时候可能会有点复杂,下面是我的代码,仅供参考
if (JSON.stringify(this.$Route.query) != "{}") {
console.log('这是分享数据', this.$Route.query)
} else if (options != undefined) {
console.log('这是分享数据1', options)
}else{
const pages=getCurrentPages()
// 当前页面
const page = pages[pages.length - 1]
console.log('这是分享数据2',page.options)
}