uni.app 使用 mixins 技术统一注入小程序页面分享到好友,分享朋友圈功能
1. 在utils文件夹下创建share.js
export default{
data(){
return {
mixinsShare:{
title:'首页',
path:'/pages/index/index',
imageUrl:'',
desc:'',
content:''
}
}
},
onShareAppMessage(res) {
if(!this.mixinsShare.path){
this.mixinsShare.path=getCurrentPages()[0].$page.fullPath
}
return {
title:this.mixinsShare.title,
path:this.mixinsShare.path,
imageUrl:this.mixinsShare.imageUrl,
desc:this.mixinsShare.desc,
content:this.mixinsShare.content,
success(res){
uni.showToast({
title:'分享成功'
})
},
fail(res){
uni.showToast({
title:'分享失败',
icon:'none'
})
}
}
},
onShareTimeline(res) {
if(!this.mixinsShare.path){
this.mixinsShare.path=getCurrentPages()[0].$page.fullPath
}
return {
title:this.mixinsShare.title,
path:this.mixinsShare.path,
imageUrl:this.mixinsShare.imageUrl,
desc:this.mixinsShare.desc,
content:this.mixinsShare.content,
success(res){
uni.showToast({
title:'分享成功'
})
},
fail(res){
uni.showToast({
title:'分享失败',
icon:'none'
})
}
}
}
}
2. 为所有页面挂载
import share from "./utils/share.js"
Vue.mixin(share)
3.单独页面使用
import share from "@/utils/share.js"
export default {
mixins:[share],
data() {
return {
mixinsShare: {
title: '首页',
path: 'pages/index/index',
desc: '描述',
content: '内容',
imageUrl:'',
},
}
}
}