利用uni-app的onShareAppMessage与onShareTimeline来实现的分享给朋友或分享朋友圈!

onShareAppMessage:官网详细讲解地址:

onShareTimeline:官网详细讲解地址:

onShareTimeline:是生命周期函数,与onLoad、onShow同级使用。主要 用于监听用户点击右上角转发到朋友圈

分享好友

export default {
    data() {
        return {
        
        }
    },
    onLoad: function () {
    
    },
    onShareAppMessage(res) {
            if (res.from === 'button') {
                console.log("来自页面内按钮分享")
             }
            return {
                path:"/pages/index/index",  //不设置默认当前页面
                title: "我是标题", 
                imageUrl:" " //不设置默认截取页面
             }
    },
    methods: {
    
    }
}

此时小程序右上角的分享已生效,如有需要也可以添加 share 属性的按钮触发分享操作

<button  type="warn"  open-type="share" >分享</button>

分享到朋友圈:

onShareTimeline(){
	return {
      	path:"/pages/index/index",  //不设置默认当前页面
        title: "我是标题", 
        imageUrl:" " //不设置默认截取页面
    }
},