小程序右上角三个点,如果没有设置分享,则是灰色的,需要设置
第一:指定页面分享
onShareAppMessage(res) {
return {
title: '自定义分享标题',
path: '/pages/test/test?id=123'
}
}
onShareAppMessage 与 页面的onLoad 同级,这个是指定页面可以配置其他参数 详情见文档:https://uniapp.dcloud.io/api/plugins/share?id=onshareappmessage
第二:页面内分享,通过按钮button分享
<button open-type="share">分享</button>
onShareAppMessage(res) {
if (res.from === 'button') {// 来自页面内分享按钮
console.log(res.target)
}
return {
title: '自定义分享标题',
path: '/pages/test/test?id=123'
}
}
第三:全局分享,如果需要实现所有页面都分享到一个页面,每个页面都写很麻烦,可以这样实现
来源链接:https://blog.csdn.net/qq_33836424/article/details/107911648
首先,新建混入mixin-share.js
export default {
data() {
return {
//这个share可以在每个页面的data中设置相应的转发内容
share:{
title:'自定义标题',
path:'/pages/home/index',
}
}
},
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'
})
}
}
}
}
其次,在main.js 里面注入全局
import share from'./utils/mixin-share.js';
Vue.mixin(share)
结束,最后测试通过