上個月在做小程序的項目時,甲方需要給小程序添加個分享的功能,查看uniapp官方文檔后,發現uniapp有自帶的小程序分享功能(https://uniapp.dcloud.io/api/plugins/share),里面一堆的參數介紹,你們自己看看吧。我這里就自己封裝了一個,哪個頁面需要就在哪個頁面調用
1、創建一個js文件(share.js)
export default{ data(){ return { //設置默認的分享參數 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' }) } } } }
2、全局使用, 在 main.js
里面 添加全局的 mixin
import share from '@/....你的路徑.../share.js'
Vue.mixin(share)
3、在需要的頁面進行調用就行啦
export default { data(){ return { //設置默認的分享參數 share:{ title:'ALAPI', path:'/pages/index/index', imageUrl:'', desc:'', content:'' } } },