微信小程序全局设置分享内容

微信小程序每个页面都可以在onShareAppMessage中设置分享内容,如果想要全局设置成一样的分享内容如何设置呢?

在app.js中新增以下方法:

 1     //重写分享方法
 2     overShare: function () {
 3         //监听路由切换
 4         //间接实现全局设置分享内容
 5         wx.onAppRoute(function (res) {
 6             //获取加载的页面
 7             let pages = getCurrentPages(),
 8                 //获取当前页面的对象
 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: '标题',
20                             path: '/pages/nearby/index'
21                         };
22                     }
23                 }
24             }
25         })
26     },

然后在onLaunch中调用即可,如果你有更好的实现方案,请告诉我.

posted @ 2019-05-20 16:19  魔狼再世  阅读(17872)  评论(2编辑  收藏  举报