1. 程式人生 > 其它 >微信小程式新增分享功能(分享給朋友) uniapp版

微信小程式新增分享功能(分享給朋友) uniapp版

先建立一個.js檔案

  程式碼如下

export default{
    data(){
        return {
            //設定預設的分享引數
            share:{
                title:'XXXX',
                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:'分享成功', icon:
'success', mask: true, duration: 1200 }) }, fail(res){ uni.showToast({ title:'分享失敗', icon:'none', mask: true, duration: 1200 }) } } } }

然後在main.js中引入即可

import share from '@/js/share.js'

Vue.mixin(share)

這樣就可以了