注意:僅支持分享微信小程序到微信聊天界面,想進入朋友圈需改為分享圖片方式,在圖片中包含小程序碼。一般通過canvas繪制圖片,插件市場有很多生成圖片的插件。
小程序分享:
// 小程序分享 onShareAppMessage() { return { title: 'test', path: 'pages/index/index' } }
只有定義了此事件處理函數,小程序右上角菜單才會顯示“轉發”按鈕。
App分享:
首先要在 manfest.json中的 App模塊權限配置 中,勾選上分享
然后在 AppSDK配置 中,設置分享模塊,需要填寫對應的信息。
添加按鈕:
<!-- #ifdef APP-PLUS --> <button type="primary" @click="share">分享</button> <!-- #endif -->
方法:
// 分享好友 share() { // 分享圖文到微信聊天界面 uni.share({ provider: "weixin", // 服務商 scene: "WXSceneSession", // 場景 微信好友WXSceneSession 朋友圈WXSceneTimeLine type: 0, // 圖文0 文字1 圖片2 href: "http://uniapp.dcloud.io/", // 分享h5地址 title: "uni-app分享", summary: "我正在使用HBuilderX開發uni-app,趕緊跟我一起來體驗!", // 描述 imageUrl: "https://img-cdn-qiniu.dcloud.net.cn/uniapp/images/uni@2x.png", success: function (res) { console.log("success:" + JSON.stringify(res)); }, fail: function (err) { console.log("fail:" + JSON.stringify(err)); } }); }
分享圖文:href、imageUrl 為必選參數,title/summary 二選一,最好將這四個參數都選上。
更多分享,請參考文檔:
https://uniapp.dcloud.io/api/plugins/share?id=share
