小程序中实现点击一个转发按钮,携带当前点击参数,转发另一个页面的功能

微信官方文档新提供了button的open-type="share"属性,然后js中直接定义onShareAppMessage()方法,旨在更直观的触发用户转发。但是有时候需要转发另一个页面,并且需要将当前点击对象的信息传递到要转发的页面。下面就来展示一下实现过程:

wxml:

<button
   class="zanbtn shared"
   data-item="{{comment}}"
   open-type="share"
   hover-class="none">
   <image
      class='zanIcon commentIcon'
      src="/assets/images/shared.png" />
</button>

js:

onShareAppMessage(options) {
    const that = this;
    const { userInfo } = that.data;
    const item = options.target.dataset.item;

    return {
      title: `${userInfo.nickname}推荐给你一本适合${item.book.maxAgeReadPercent[0]}岁的好书`,
      path: 'pages/recommends/share/index?id=' + item.id,
    };
},

pages/recommends/share/index.js:

loadOptions: {},
onLoad (options) {

  // 接收转发参数,赋值到loadOptions对象中
  this.loadOptions = options;
},

猜你喜欢

转载自blog.csdn.net/liya_nan/article/details/81634695