微信小程序的分享内容,可以在某个页面单独设置,也可以在App.vue中全局设置
1.单独页面设置分享内容:
onShareAppMessage(){//发送给朋友
return {
title: this.goodsDet.goodsName,//分享链接的标题
path: '/pages/index/shop/goodsDet?goodsId=' + this.goodsId,//分享链接指定的页面
imageUrl: this.goodsDet.goodsPicture[0],分享链接的封面
}
},
onShareTimeline(res) { //分享到朋友圈
return {
title: this.goodsDet.goodsName,
path: '/pages/index/shop/goodsDet?goodsId=' + this.goodsId,
imageUrl: this.goodsDet.goodsPicture[0],
}
},
2.全局配置分享内容
第一步:在App.vue中的golbalData中配置全局分享内容
globalData:{
overShare: () => {
//监听路由切换, 间接实现全局设置分享内容
uni.onAppRoute(res => {
console.log("间接实现全局设置分享内容");
//获取加载的页面
const pages = getCurrentPages(),
//获取当前页面的对象
view = pages[pages.length - 1];
console.log(view,'view--')
if (view) {
//全局触发 显示出分享具栏
wx.showShareMenu({
withShareTicket: true,
//设置下方的Menus菜单,才能够让发送给朋友与分享到朋友圈两个按钮可以点击
menus: ["shareAppMessage", "shareTimeline"]
})
view.onShareAppMessage = () => {
//分享配置
return {
title: '花骑士代驾',//分享链接的标题
path: '/pages/index/index',//分享链接指定的页面
imageUrl: '/static/images/logo.png',//分享链接的封面
success(res) {
uni.showToast({
title: '分享成功'
})
},
fail(res) {
uni.showToast({
title: '分享失败',
icon: 'none'
})
}
};
};
}
})
},
}
第二步:在App.vue的onLaunch中调用
onLaunch(){
this.globalData.overShare();
}