微信小程序一个页面多个按钮分享怎么处理

时间:2023-03-09 12:45:22
微信小程序一个页面多个按钮分享怎么处理

首先呢,第一步先看api文档:

组件:button

https://developers.weixin.qq.com/miniprogram/dev/component/button.html

框架-逻辑层-注册页面-页面事件处理函数:onShareAppMessage

https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/page.html#%E9%A1%B5%E9%9D%A2%E4%BA%8B%E4%BB%B6%E5%A4%84%E7%90%86%E5%87%BD%E6%95%B0

监听用户点击页面内转发按钮(<button> 组件 open-type="share")或右上角菜单“转发”按钮的行为,并自定义转发内容。

代码区html

<view>
<button open-type='share' id="1">1</button>
<button open-type='share' id="2">2</button>
</view>

代码区javascript

  /**
* 用户点击右上角分享
*/
onShareAppMessage: function (res) {
if (res.from === 'button') {
// 来自页面内转发按钮
if (res.target.id == 1) {
return {
title: '自定义1111转发标题',
path: '/page/user?id=123'
}
}
if (res.target.id == 2) {
return {
title: '自定义22222转发标题',
path: '/page/user?id=123'
}
}
} else {
return {
title: '自定义转发标题',
path: '/page/user?id=123'
} } }

以上代码主要是通过button按钮组件的id来进行区分的,在javascript中的onShareAppMessage中的res.target.id加以区分,同一个页面,去分享多个功能的例子。