小程序: 右上角的分享

1. 页面 js 中: 只有添加了  onShareAppMessage( ),该页面才能被分享

2. onShareAppMessage( )中的设置: 需要return { }

onShareAppMessage: function () {
     var that = this;
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    return {
      title: "",        // 默认是小程序的名称
      path: `/pages/enjoy/enjoy?type=${that.data.type}&id=${that.data.id}`,        // 默认是当前页面,必须是以‘/’开头的完整路径
      imgUrl: '',     //自定义图片路径,可以是本地文件路径、代码包文件路径或者网络图片路径,支持PNG及JPG,不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
        success: function(res){
        // 转发成功之后的回调
        if(res.errMsg == 'shareAppMessage:ok'){
               console.log('转发成功')
        }
      },
      fail: function(){
        // 转发失败之后的回调
        if(res.errMsg == 'shareAppMessage:fail cancel'){
                console.log('用户取消转发')
        } else if(res.errMsg == 'shareAppMessage:fail'){
                console.log('转发失败')
        }
      },
       complete: function() {
        // 转发结束之后的回调(转发成不成功都会执行)
      }
      }
  }
View Code

其中: title -- 设置转发时的标题

            path -- 打开分享时跳转到的页面

            imgUrl -- 设置转发时的图片

3. 获取携带的参数 

    

// 点击分享进入的页面 -- 即 path中指向的页面
Pages({

   onload: function(options) {
       console.log(options)  // 获取到携带的参数
   }
 
})
View Code

猜你喜欢

转载自www.cnblogs.com/ljr210/p/9908835.html