页面按钮
<!-- 必须,button按钮 data-name="shareBtn" open-type="share" ,data-obj分享参数 -->
<button data-name="shareBtn" open-type="share" :data-obj="goodDetail">
<view class="function_text">分享</view>
</button>
封装share.js(在项目文件夹下创建utils文件夹,创建share.js文件)
export default {
data() {
return {
// 默认的全局分享内容
share: {
title: '古蜀笔记',
path: '/pages/index', // 全局分享的路径,比如 首页
// imageUrl: 'https://s3.dova.link:3001/test/20231225/c74c4f34e7bf48d3b56fb27c368e7180.jpg', // 全局分享的图片(可本地可网络)
}
}
},
// 定义全局分享
// 1.发送给朋友
onShareAppMessage(res) {
console.log(res, '获取到的参数');
let obj = {
title: this.share.title,
path: this.share.path,
// imageUrl: this.share.imageUrl,
}
if (res.from == "button") {// 判断是按钮发起的分享(button)还是右上角小程序胶囊发起的分享(menu)
let pageInfo = res.target.dataset.obj // 页面传过来的参数(goodDetail)
var pages = getCurrentPages() // 获取栈实例
let currentPage = pages[pages.length - 1]['$page']['fullPath'] //当前页面路径(带参数)
obj.title = pageInfo.name
obj.path = currentPage
obj.imageUrl = pageInfo.homePhoto
}
return obj
},
//2.分享到朋友圈
onShareTimeline(res) {
return {
title: this.share.title,
path: this.share.path,
// imageUrl: this.share.imageUrl,
}
},
}
main.js引入
// 导入并挂载全局的分享方法
import share from '@/utils/share.js'
Vue.mixin(share)