uniapp 微信小程序全局分享

uniapp 微信小程序全局分享

mixins\share.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
export default {
data () {
return {
// 分享内容配置
share: {
title: '默认标题',
},
// 是否打开分享功能
share_show: true,
}
},
created () {
// 创建的时候判断要不要隐藏
this.handleShareHide();
},
watch: {
share_show () {
// 被修改时判断要不要隐藏
this.handleShareHide();
},
},
methods: {
// 隐藏分享功能
handleShareHide () {
if(!this.share_show) {
return uni.hideShareMenu();
}
},
},
onShareAppMessage () {
return {
title: this.share.title,
};
},
}

main.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import Vue from 'vue'
import App from './App'
// 引入
import share from 'mixins/share.js'

Vue.config.productionTip = false

App.mpType = 'app'

// 全局注册
Vue.mixin(share)

const app = new Vue({
...App
})
app.$mount()

使用

pages\index\index.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<template>
<view class="container">分享测试</view>
</template>

<script>
export default {
data () {
return {
share: {
title: '新标题', // 标题覆盖
},
share_show: false, // 关闭分享
}
},
onShow () {
this.share_show = true;
},
}
</script>
往上