[TOC]
## 微信自帶分享
### 分享到好友或群 第一種
```
//右上角三點創建分享
wx.onMenuShareAppMessage({
title: '', // 分享標題
desc: '', // 分享描述
link: '', // 分享鏈接,該鏈接域名或路徑必須與當前頁面對應的公眾號JS安全域名一致
imgUrl: '', // 分享圖標
type: '', // 分享類型,music、video或link,不填默認為link
dataUrl: '', // 如果type是music或video,則要提供數據鏈接,默認為空
success: function () {
// 用戶確認分享后執行的回調函數
},
cancel: function () {
// 用戶取消分享后執行的回調函數
}
});
```
### 第二種 通過按鈕分享
```
<button style="border: none;background:none;padding-left: 0;line-height: 1;margin: 0;" open-type="share">
添加 open-type=“share” 就可以調用 js 里的函數
onShareAppMessage: function (ops) {
if (ops.from === 'button') {
// 來自頁面內轉發按鈕
console.log(ops.target)
}
return {
title: 'xx小程序',
path: 'pages/index/index',
//通過分享進入的頁面路徑
success: function (res) {
// 轉發成功
console.log("轉發成功:" + JSON.stringify(res));
},
fail: function (res) {
// 轉發失敗
console.log("轉發失敗:" + JSON.stringify(res));
}
}
},
```