微信小程序一個頁面多個按鈕分享怎么處理


首先呢,第一步先看api文檔:

組件:button

https://developers.weixin.qq.com/miniprogram/dev/component/button.html

框架-邏輯層-注冊頁面-頁面事件處理函數:onShareAppMessage

https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/page.html#%E9%A1%B5%E9%9D%A2%E4%BA%8B%E4%BB%B6%E5%A4%84%E7%90%86%E5%87%BD%E6%95%B0

監聽用戶點擊頁面內轉發按鈕(<button> 組件 open-type="share")或右上角菜單“轉發”按鈕的行為,並自定義轉發內容。

代碼區html

<view>
    <button open-type='share' id="1">1</button>
    <button open-type='share' id="2">2</button>
</view>

代碼區javascript

  /**
    * 用戶點擊右上角分享
    */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 來自頁面內轉發按鈕
      if (res.target.id == 1) {
        return {
          title: '自定義1111轉發標題',
          path: '/page/user?id=123'
        }
      }
      if (res.target.id == 2) {
        return {
          title: '自定義22222轉發標題',
          path: '/page/user?id=123'
        }
      }
    } else {
      return {
        title: '自定義轉發標題',
        path: '/page/user?id=123'
      }

    }

  }

以上代碼主要是通過button按鈕組件的id來進行區分的,在javascript中的onShareAppMessage中的res.target.id加以區分,同一個頁面,去分享多個功能的例子。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM