无论是使用右上角的三点进行分享还是使用按钮进行分享,都可以使用。
注意按钮分享需要使用open-type="share",需要传递参数的还要加:data-obj="obj"
封装share.js
export default {
name: "share",
data() {
return {
}
},
onLoad: function() {
wx.showShareMenu({
withShareTicket: true,
menus: ["shareAppMessage", "shareTimeline"]
})
},
//发送给朋友
onShareAppMessage(res) {
let that = this;
if (res.from === 'button') {
let obj = res.target.dataset.obj // 获取 button 组件 自定义的data-obj值
//这块需要传参,不然链接地址进去获取不到数据
// let path = `/` + that.$scope.route + `?item=` + that.$scope.options.item;
let path = ''
return {
title: `${obj.filename}`,
path: path,
imageUrl: ''
};
}
// 右上角三点
if (res.from === 'menu') {
return {
title: '跟踪云',
path: '',
imageUrl: ''
};
}
},
// 分享到朋友圈
onShareTimeline() {
return {
title: '跟踪云',
path: '',
imageUrl: ''
};
},
methods: {
}
}
全局使用mian.js
import share from '@/utils/share.js'
Vue.mixin(share)
const app = new Vue({
...App,
share
})
到这就不需要在页面中写任何,直接就点击分享按钮或者是右上角三点就可以分享了。