主要参考了这位博主的博客: uniapp 实现微信小程序全局分享及自定义分享按钮样式_uniapp 小程序分享_Koreyoshi·的博客-CSDN博客
share.js文件的代码:
export default {
data() {
return {
//设置默认的分享参数
share: {
title: '培养电商新农人,发现乡村好主播',
path: '/pages/home/home',
imageUrl: 'https://znyzfresource.oss-cn-shanghai.aliyuncs.com/images/uploads/ccb-min/share.png',
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'
})
}
}
}
// onShow() {
// // 可以分享的页面
// let allowSharePage = [
// {
// path: 'pages/discover/discover'
// },
// {
// path: 'subpkg/aboutAs/aboutAs'
// },
// {
// path: 'subpkg/newsList/newsList'
// },
// {
// path: 'subpkg/collectList/collectList'
// },
// {
// path: 'subpkg/announcementList/announcementList'
// },
// {
// path: 'subpkg/newsDetails/newsDetails'
// }
// ]
// // 微信分享的路径 修改为 当前页面的路径
// let routes = getCurrentPages() // 获取当前打开过的页面路由数组
// if (routes.length > 0) {
// let curRoute = routes[routes.length - 1] // 获取当前页面路由,也就是最后一个打开的页面路由
// let page = allowSharePage.find((e) => {
// if (e.path == curRoute.route) return true
// })
// if (page != undefined) {
// this.share = {
// path: curRoute.$page.fullPath,
// title: '培养电商新农人,发现乡村好主播',
// imageUrl: '',
// desc: '',
// content: ''
// }
// }
// }
// }
}