小程序全局分享配置
方法一,本人验证不生效
App({
onLaunch: function(options) {
this.onShareAppMessage()
},
onShareAppMessage() {
// 监听路由变化
wx.onAppRoute((res) => {
const page = getCurrentPage()
if (page) {
// 这里和下面 onShareAppMessage, onShareTimeLine 二选一
wx.showShareMenu({
withShareTicket: true,
menus: ['shareAppMessage', 'shareTimeline'],
})
// ! 模拟器可能不生效,在真机查看
page.onShareAppMessage = () => {
{
return {
title: 'xxxxx!',
path: `pages/index/index`,
imageUrl: 'https://www.example.com/share.png',
}
}
}
page.onShareTimeline = () => {
return {
title: 'xxxxx!',
query: `a=1&b=2`,
}
}
}
})
},
})
方法二,亲测可用,推荐
// 方法二,此方法验证可行 date:2022-5-20
// app.js
!(function () {
var PageOld = Page
Page = function (options) {
options = Object.assign(
{
getIdCustomer() {
return getApp().globalData.idCustomer
},
onShareAppMessage() {
const params = encodeURIComponent(`type=1&val=${this.getIdCustomer()}`)
{
return {
title: '惺惺惜惺惺!',
path: `/${this.route}?qp=${params}`,
imageUrl: config.imagePrexDoamin + '/share.png',
}
}
},
onShareTimeline() {
const params = encodeURIComponent(`type=3&val=${this.getIdCustomer()}`)
return {
title: '嘻嘻嘻嘻!',
query: `qp=${params}`,
}
},
},
options
)
PageOld(options)
}
})()
本文介绍了两种在微信小程序中配置全局分享的方法,一种是可能导致模拟器失效,而另一种则是亲测有效的解决方案,包括`onShareAppMessage`和`onShareTimeline`的定制,适合开发者优化分享体验。
3434

被折叠的 条评论
为什么被折叠?



