微信小程序组件分享的坑
1.在使用微信小程序时,有时候会使用到分享功能,一般直接分享页面或者分享某个自定义页面,可以使用他默认的分享,只需要在page({})事件中添加一个方法就可以使用;
onShareAppMessage: function (res) {}
2.但是有时候会使用到组件模块,分享又只能写在模块中,就发现分享只能分享默认的页面,而不能自定义,Component({})事件中不支持直接使用onShareAppMessage: function (res) {}方法;但是可以在组件页面调用父级的分享;
可以在组件中使用
不对他在组件中进行操作,而是直接在父级的page({})方法下使用onShareAppMessage: function (res) {}方法 即可自定义分享功能;
自定义分享例子:
onShareAppMessage: function (res) {
return {
title: ‘我是自定义分享’,
path: “我是分享的地址”,
imageUrl: ‘我是分享图片’,
}
},
还有另一种分享默认至朋友圈或者微信好友的自定义:
shareAppMessage(微信好友)
shareTimeline(朋友圈)
onShareAppMessage(e) {
wx.showShareMenu({
withShareTicket: true,
menus: [‘shareAppMessage’, ‘shareTimeline’]
})
},
或者自定义:
先可以分享到微信好友,然后再分享到朋友圈;
onShareTimeline: function () {
return {
title: ‘我是分享到朋友圈’,
imageUrl: ‘分享图片地址’
}
},