首先呢,第一步先看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加以區分,同一個頁面,去分享多個功能的例子。