微信小程序每個頁面都可以在onShareAppMessage中設置分享內容,如果想要全局設置成一樣的分享內容如何設置呢?
在app.js中新增以下方法:
App({
onLaunch: function () {
this.overShare()
},
//重寫分享方法
overShare: function () {
//監聽路由切換
//間接實現全局設置分享內容
wx.onAppRoute(function (res) {
//獲取加載的頁面
let pages = getCurrentPages(),
//獲取當前頁面的對象
view = pages[pages.length - 1],
data;
if (view) {
data = view.data;
// console.log('是否重寫分享方法', data.isOverShare);
if (!data.isOverShare) {
data.isOverShare = true;
view.onShareAppMessage = function () {
//你的分享配置
return {
title: '學事通升學匯',
path: '/pages/index/index',
imageUrl: 'https://proxy.shunzhi.net:51315/mp_share.png',
};
}
}
}
})
}
})
然后在onLaunch中調用即可。