上個(gè)月在做小程序的項(xiàng)目時(shí),甲方需要給小程序添加個(gè)分享的功能损搬,查看uniapp官方文檔后,發(fā)現(xiàn)uniapp有自帶的小程序分享功能(https://uniapp.dcloud.io/api/plugins/share)咬腕,里面一堆的參數(shù)介紹壶硅,你們自己看看吧。我這里就自己封裝了一個(gè)瓦呼,哪個(gè)頁(yè)面需要就在哪個(gè)頁(yè)面調(diào)用
- 創(chuàng)建一個(gè)js文件(share.js)
export default{
data(){
return {
//設(shè)置默認(rèn)的分享參數(shù)
share:{
title:'ALAPI',
path:'/pages/index/index',
imageUrl:'',
desc:'',
content:''
}
}
},
onShareAppMessage(res) {
return {
title:this.share.title,
path:this.share.path,
imageUrl:this.share.imageUrl,
desc:this.share.desc,
content:this.share.content,
success(res){
uni.showToast({
title:'分享成功'
})
},
fail(res){
uni.showToast({
title:'分享失敗',
icon:'none'
})
}
}
}
}
- 全局使用, 在 main.js 里面 添加全局的 mixin
import share from '@/....你的路徑.../share.js'
Vue.mixin(share)
3.在需要的頁(yè)面進(jìn)行調(diào)用就行啦
export default {
data(){
return {
//設(shè)置默認(rèn)的分享參數(shù)
share:{
title:'ALAPI',
path:'/pages/index/index',
imageUrl:'',
desc:'',
content:''
}
}
},