uniapp自定义全局分享好友
vue mixins
创建mixins目录
- 在项目根目录下创建
mixins
目录 - 新建
share.js
文件
export default {
data() {
return {
shareData: {
title: '好东西一起分享~~~',
imageUrl: '',
},
}
},
onLoad() {
let tmpUserId = this.$store.state.vuex_userInfo.id;
this.invite = tmpUserId ? tmpUserId : '';
// this.shareData = this.$store.state.commShareData;
},
onShareAppMessage(res) { //发送给朋友
return {
title: this.shareData.shareTitle, //
imageUrl: this.shareData.sharePosterUrl,
path: 'pages/index/index' + this.invite
}
},
onShareTimeline(res) { //分享到朋友圈
return {
title: this.shareData.title,
imageUrl: this.shareData.imageUrl,
path: 'pages/index/index?inviteCode=' + this.invite
}
}
}
使用
全局引入
在
main.js
中引入
import { share } from './mixins/share.js'
Vue.mixin(share)
局部引入
在需要分享的页面引入
import mixin from '../../mixins/share.js'
export default {
mixins: [mixin],
}