分享流程:
- 小程序点击右上角三个点,会弹出弹框,发现里面有分享,此时是置灰状态,不可以使用
- 要调用onShareAppMessage这个api分享按钮才可以使用
- 可能有多个地方需要分享,所以我们要通过mixins的混入,在不同模块进行,引入使用
- 在项目根目录新建minxins目录,然后在mixins目录里新建share.js文件
- 最后在main.js中引入share.js,通过Vue.minxin()注册
上代码:
minxins/share.js
export default {
// 点击右上角分享
onShareAppMessage(res) {
if (res.from === 'button') {
// 点击button按钮分享
console.log(res.from);
} else {
// 点击右上角三个点进行分享
return {
title: '蛤蟆倒钩', // 自定义转发标题
path: '/pages/home/home', // 点击查看分享页跳转的页面
imageUrl: '' // 分享时展示的图片地址
}
}
},
// 点击右上角朋友圈
onShareTimeline(res) {
},
// 点击右上角收藏
onAddToFavorites() {
}
}
main.js
import App from './App'
// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
import share from '@/mixin/share.js'
Vue.mixin(share)
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
})
app.$mount()
// #endif
// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
const app = createSSRApp(App)
return {
app
}
}
// #endif
在vue.js中引入
<script>
import share from '@/minxins/store.js'
export default {
mixins: [share],
data() {
return {
}
}
}
</script>