微信小程序分享中分为点击右上角的分享和点击按键(即是button组件)的分享.而不同按键的分享可以通过id属性来区分,具体实现如下:
.js页面中代码
//分享功能
onShareAppMessage: function (res) {
var that = this;
//分享的类型为按键类型
if (res.from == "button") {
//分享为按键中的求助即id=1
if (res.target.id == 1) {
return {
title: '按键1要分享的标题',
path: '/pages/index/index',
success: function (res) {
}
}
}
//分享为按键中的分享即id=2
if (res.target.id == 2) {
return {
title: '按键2要分享的标题',
path: '/pages/index/index',
success: function (res) {
}
}
}
}
//分享类型中右上角的分享
else {
return {
title: '点击右上角要分享的标题',
path: '/pages/index/index',
success: function (res) {
}
}
}
}
在wxml页面中代码:
<button class="share-btn" open-type="share" id="1"></button><!-- 按键1 -->
<button class="share-btn" open-type="share" id="2"></button><!-- 按键2 -->
其中分享按钮的代码一定是如上格式,要区分不同的按钮的只需改变其中的id值来区分.
综上,在分享函数onShareAppMessage:function(res){ } 函数中,回调参数具体功能如下
res.from = 'menu' 表示右上角的点击分享(或称转发)
res.from = 'button' 表示是按键触发的分享
res.target.id 的值是表示按键button标签中的id的值.