小程序wx.showActionSheet 调起转发、分享

在微信小程序中,wx.showActionSheet无法直接调起转发功能。正确的做法是在button组件中设置open-type='share',并配合page.onShareAppMessage方法。如果需要自定义action-sheet,可以通过map进行控制。在labrador框架下,使用this.setState更新状态,xml中引用页面数据应为{{state.variable}}。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


wx.showActionSheet() 方法中无法调取转发功能。
因为转发必须是 button 按钮 设置open-type="share", 会调取page.onShareAppMessage()。

自定义action-sheet

<!-- 定义一个action-sheet, 通过hidden控制显示隐藏。 -->
<action-sheet hidden="{{state.actionSheetHidden}}" bindchange="actionSheetChange"> 

  <!-- action-sheet-item 是每个 item -->
  <action-sheet-item class="item" data-id="{{state.itemData.shop_id}}">
      <button open-type="share">{{state.actionSheetItems[0]}}        
      </button>
  </action-sheet-item>

  <action-sheet-item bindtap="showShareImageModal">{{state.actionSheetItems[1]}}</action-sheet-item>

  <!-- action-sheet-cancel 是取消按钮。 -->
  <action-sheet-cancel>取消</action-sheet-cancel>
</action-sheet>

 


或者通过map进行控制。

.xml

<!-- actionSheetItems: [{bindTap: 'Menu1', text: "发给微信好友"}, {bindTap: 'Menu2', text: "生成分享图片"}]; -->

<action-sheet hidden="{{state.actionSheetHidden}}" bindchange="actionSheetChange">
  <!-- 自定义 转发按钮 -->
  <action-sheet-item class="item">
    <button open-type="share">{{state.actionSheetItems[0].text}}</button>
  </action-sheet-item>

  <!-- 剩下的item -->
  <block wx:for-items="{{state.actionSheetItems}}">
    <action-sheet-item bindtap="actionSheet{{item.bindTap}}" wx:if="{{index !== 0}}">{{item.text}}</action-sheet-item>
  </block>

  <!--取消按钮-->
  <action-sheet-cancel>取消</action-sheet-cancel>
</action-sheet>

 

.js

// 右上角分享按钮, 点击弹出弹窗,选择发给微信好友 或 生成分享图片。
var shareModeData = [{bindTap: 'Menu1', text: "发给微信好友"}, {bindTap: 'Menu2', text: "生成分享图片"}];
constructor(props) {
  super(props);
  this.state = {
    actionSheetHidden: true, // 控制分享弹窗modal
    actionSheetItems: shareModeData, // 弹窗展示数据
  }
}
onShareAppMessage() {
  let share_url = '/pages/index/index'
  let imageUrl = 'https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=1650310964,3049609459&fm=173&app=25&f=JPEG?w=439&h=401&s=17B056855A6786CC443835B90300C001'
  return {
    title: '我发现了一个不错的商品,快来看看吧!~~'
    path: share_url,
    imageUrl: imageUrl,
    success: (res) => {
      // 转发成功
      console.log("分享成功", res)
    },
    fail: (err) => {
      // 转发失败
      console.log("分享失败", err)
    }
  }
}
// 显示分享弹窗。
showShareModal(e) {
  this.setState({
    actionSheetHidden: !this.state.actionSheetHidden
  });
}
// 改变 action-sheet状态。
actionSheetChange(e) {
  this.setState({
    actionSheetHidden: !this.state.actionSheetHidden
  });
}
// 分享弹窗 中item bindTap对应的方法。
actionSheetMenu2() {
  this.showShareImageModal();
}

 


使用的labrador 框架
所以js中用的不是this.setDate, 而是this.setState。
xml 中取page中的数据不是直接 {{variable}}, 而是{{state.variable}}。

转载于:https://www.cnblogs.com/VinsonH/p/9459386.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值