微信小程序分享功能的實現方法有兩種:
第一種
在page.js中實現onShareAppMessage,便可在小程序右上角選擇分享該頁面
onShareAppMessage: function () { return { title: '彈出分享時顯示的分享標題', desc: '分享頁面的內容', path: '/page/user?id=123' // 路徑,傳遞參數到指定頁面。 } }
第二種
自定義按鈕實現分享,在page中添加一個帶有open-type=’share’的button標簽(<button open-type=’share’><\/button>)。點擊該按鈕后,即會自動觸發已經在page.js中定義好的onShareAppMessage方法,實現分享功能。
<button open-type='share'>分享</button>
獲取分享傳遞的參數
如上例,path屬性指向的是user頁面,並附帶id=123的參數。我們只需在user.js的onLoad函數中,通過options查看傳遞過來的參數:
// user.js Page({ onLoad: function(options) { console.log(options); } })
轉: https://blog.csdn.net/sinat_41917956/article/details/80606459