在做小程序项目的时候,遇到一个需求,一个页面多个按钮分享不同的内容,也查了一下资料,根据自己的项目的需求,自己做了一个,在这里记录记录。如图
wxml代码
<view class="inviteWay-layout">
<view class="item">
<view class="share white lineHeight relative">
<text class="font24 copy">复制这条文案:{{shareText}}</text>
<text class="copyBtn font24" data-text="{{share_content}}" bindtap="copyTap">复制文案</text>
</view>
</view>
<view class="item" wx:for="{{share_list}}" wx:key="index">
<view class="share white clearfix">
<image class="fl img" src="{{item.share_image}}"></image>
<text class="font24 cont fl text-two">{{item.title}}</text>
<text class="linkBtn fr font24" bindtap="shareTap">分享文案</text>
<button class='shareBtn' open-type="share" data-id="{{item.aid}}" data-url="{{item.share_image}}" data-title="{{item.title}}"></button>
</view>
</view>
</view>
data-id是自己需要传的参数id data-url 分享的图片 data-title 分享的标题
js
/**
* 用户点击右上角分享
*/
onShareAppMessage: function (e) {
var that = this
// if (res.from === 'button') {
// // 来自页面内转发按钮
// console.log(res.target)
// }
if(e.target.dataset.id>0){
return {
title: e.target.dataset.title, // 分享名称
path: '/pages/article/article?member_id=' + wx.getStorageSync("uid") + '&&id=' + e.target.dataset.id, // 点击分享后的链接要来到的页面的路径已经对应需要的参数
imageUrl: e.target.dataset.url,
// 分享成功之后的操作
success: function (res) {
console.log("分享成功:" + JSON.stringify(res));
},
// 分享失败之后的操作
fail: function (res) {
console.log("分享失败:" + JSON.stringify(res));
}
}
}else{
return {
title: e.target.dataset.title, // 分享名称
path: '/pages/index/index?member_id=' + wx.getStorageSync("uid"), // 点击分享后的链接要来到的页面的路径已经对应需要的参数
imageUrl: e.target.dataset.url,
// 分享成功之后的操作
success: function (res) {
console.log("分享成功:" + JSON.stringify(res));
},
// 分享失败之后的操作
fail: function (res) {
console.log("分享失败:" + JSON.stringify(res));
}
}
}
}
这里的参数都是从后台获取过来的,也可以写成死数据