微信小程序每個(gè)頁面都可以在onShareAppMessage中設(shè)置分享內(nèi)容,如果想要全局設(shè)置成一樣的分享內(nèi)容如何設(shè)置呢?
在app.js中新增以下方法:
1 //重寫分享方法
2 overShare: function () {
3 //監(jiān)聽路由切換
4 //間接實(shí)現(xiàn)全局設(shè)置分享內(nèi)容
5 wx.onAppRoute(function (res) {
6 //獲取加載的頁面
7 let pages = getCurrentPages(),
8 //獲取當(dāng)前頁面的對象
9 view = pages[pages.length - 1],
10 data;
11 if (view) {
12 data = view.data;
13 console.log('是否重寫分享方法', data.isOverShare);
14 if (!data.isOverShare) {
15 data.isOverShare = true;
16 view.onShareAppMessage = function () {
17 //你的分享配置
18 return {
19 title: '標(biāo)題',
20 path: '/pages/nearby/index'
21 };
22 }
23 }
24 }
25 })
26 },
然后在onLaunch中調(diào)用即可,如果你有更好的實(shí)現(xiàn)方案,請告訴我.