微信小程序转发功能

微信小程序的右上角就有分享功能

但是必须逻辑层必须有onShareAppMessage这个方法,

/**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
View Code

 微信小程序转发官方文档:https://developers.weixin.qq.com/miniprogram/dev/api/share.html

我们可以通过给 button 组件设置属性 open-type="share",可以在用户点击按钮后触发 Page.onShareAppMessage() 事件,如果当前页面没有定义此事件,点击就没有效果。相关组件:button

微信小程序 单击按钮转发,一定要设置open-type。否则无效

 <view>
     <button open-type="share">
         <image src="../../../image/images/[email protected]"></image>
         <view class='wenxinwenzi'>微信</view>
     </button>
</view>
 onShareAppMessage: function () {
    if (ops.from === 'button') {
      // 来自页面内转发按钮
      console.log(ops.target)
    }
    return {
      title: '你收到一条信息',
      path: '转发页面的URI',
      success: function (res) {
        // 转发成功
        console.log("转发成功:" + JSON.stringify(res));
        var shareTickets = res.shareTickets;

        // //可以获取群组信息
        wx.getShareInfo({
          shareTicket: shareTickets[0],
          
        })
      },
      fail: function (res) {
        // 转发失败
        console.log("转发失败:" + JSON.stringify(res));
      }
    }
  },

猜你喜欢

转载自www.cnblogs.com/LCH-M/p/9318772.html