- 首先安装插件
npm install weixin-jsapi
- 在项目目录新建wx.js文件
wx.js代码
import wx from 'weixin-jsapi'
import {getWxConfig} from '../api/red_package/index'
const ShareImpl = function (option) {
const url = window.location.href.split("#")[0];
getWxConfig({url:url}).then(res => {
console.log(res)
wx.config({
debug: true,
appId: res.data.appId,
timestamp: res.data.timestamp,
nonceStr: res.data.nonceStr,
signature: res.data.signature,
jsApiList: [
'updateAppMessageShareData',
'updateTimelineShareData',
'getLocation',
'openLocation',
'onMenuShareAppMessage',
'onMenuShareTimeline'
]
});
wx.ready(function () {
console.log('ready执行完毕')
wx.updateAppMessageShareData({
title: option.shareTitle,
desc: option.shareDesc,
link: option.shareUrl,
imgUrl: option.shareImg
});
wx.updateTimelineShareData({
title: option.shareTitle,
desc: option.shareDesc,
link: option.shareUrl,
imgUrl: option.shareImg,
});
});
wx.error(function (res) {
console.log('error',res);
})
});
};
export default ShareImpl
- 在main.js中引入刚才写好的方法
- 在需要分享的页面中调用
mounted() {
var option = {
shareTitle : '',
shareDesc : '',
shareUrl : '',
shareImg: ''
};
this.ShareImpl(option)
},