uni-app小程序分享操作代碼


做小程序項目的時候想到了做小程序分享到微信好友這個功能,不能每個頁面寫一下這樣的話代碼冗余太厲害,所以做了一下總結,大家互相指教,一起進步,歡迎討論

1、在common中創建了一個share.js

export default{
    data(){
        return {
                       //設置默認的分享參數
            share:{
                title:'',
                path:'/pages/login/login',
                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中引入

import share from 'common/share.js';
Vue.mixin(share)

3、這樣設置以后在每個頁面內都有了分享按鈕,如果不做修改,可以直接這樣使用,若做了修改,可在每個頁面內做如下修改

export default {
data(){
        return {
                       //設置默認的分享參數
            share:{
                title:'ALAPI',
                path:'/pages/index/index',
                imageUrl:'',
                desc:'',
                content:''
            }
        }
    },

這樣就可以了,分享朋友圈同上。

參考博客:https://blog.csdn.net/qq_35432904/article/details/106330079


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM