小程序 · 页面分享
-
监听用户点击页面内转发按钮(button 组件
open-type="share"
)或右上角菜单“转发”按钮的行为,并自定义转发内容。 -
注意:只有定义了此事件处理函数,右上角菜单才会显示“转发”按钮
-
该事件处理函数需要 return 一个 Object,用于自定义转发内容
参数:
返回对象字段:
实例:
//index.wxml
...
<view class="share">
<image class="" src="/static/images/icon/share.png" mode="aspectFill" />
<text class="">分享名片</text>
//open-type必须为"share"(此次button覆盖父级view且为透明)
<button open-type="share" class="openChat"></button>
</view>
...
//index.js
Page({
/**
* 用户点击右上角分享
*/
onShareAppMessage: function(ops) {
if (ops.from === 'button') {
// 来自页面内转发按钮
// console.log(ops.target)
}
return {
title: `${this.data.userinfo[0].name}的名片`,
path: 'pages/setShare/setShare?id=2', //点击分享的图片进到哪一个页面
success: function (res) {
// 转发成功
console.log("转发成功:" + JSON.stringify(res));
},
fail: function (res) {
// 转发失败
console.log("转发失败:" + JSON.stringify(res));
}
}
},
})