wechat-小程序-分享

参考文档


触发分享的两种方式

  1. 右上角菜单

  2. 页面 自定义按钮

    只需要在 button 标签中加入 open-type="share"

    1
    2
    3
    <view class="btn-area">
    <button type="primary" open-type="share">分享</button>
    </view>

这两种方式都会触发分享, 并调用 Page.onShareAppMessage 方法


分享传递参数

  1. onShareAppMessage 定义相关参数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    onShareAppMessage: function (options) {
    if (options.from === 'button') {
    // 来自 页面自定义按钮, 可以获取到按钮相关参数
    console.log(options.target)
    }
    return {
    title: '--- aaa: 自定义转发标题',
    imageUrl: 'http://yx02.itengshe.com/testShare.jpg', // 分享图(比例是 5:4), 不定义的话默认使用当前界面的截图.
    path: '/pages/show/show?id=123&name=hello', // 分享出去后, 别人点击后跳转的页面及相关参数传递过去. 这里只能使用绝对路径.
    success: (res) => {
    console.log("--- success, res:", res)
    },
    fail: (res) => {
    console.log("--- fail, res:", res)
    }
    }
    },
  2. 测试

    a用 户分享给 b用户, b用户点击后会 直接跳转到 /pages/show/show 界面 并在 Page.onLoad 方法中可以获取到参数 idname

    1
    2
    3
    onLoad: function (options) {
    gLog("--- show onLoad", options)
    },