- 公众号添加js安全域名, 分享链接要生成二维码才能分享成卡片
- 配置全局放到App.vue
wx.config({
appId: appId, // 必填,公众号的唯一标识
timestamp: timestamp, // 必填,生成签名的时间戳
nonceStr: noncestr, // 必填,生成签名的随机串
signature: signature, // 必填,签名,
debug: true', // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
jsApiList: [
'updateAppMessageShareData',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'updateTimelineShareData'
] // 必填,需要使用的JS接口列表
})
wx.error(function(res) {
console.log('err :>> ', res)
})
- 分享页配置
// #ifdef H5
wx.ready(function() {
// 需在用户可能点击分享按钮前就先调用
wx.checkJsApi({
jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData'], // 需要检测的JS接口列表,
success: function(res) {
// 以键值对的形式返回,可用的api值true,不可用为false
// 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
console.log('checkJsApi :>> ', res)
}
})
const shareData = {
title: title, // 分享标题
desc: desc, // 分享描述
link: link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl:
imgUrl, // 分享图标
success: function() {
// 设置成功
wx.showToast({
title: '分享成功!'
})
}
}
wx.updateAppMessageShareData(shareData)
wx.updateTimelineShareData(shareData)
})
// #endif