创建了一个js,如下
export default{
data(){
return {
//设置默认的分享参数
share:{
title:'',
path:'/pages/home/home',
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'
})
}
}
},
onShareTimeline(res) {
let that = this
let data = {
title:that.share.title,
imageUrl:that.share.imageUrl,
path:that.share.path,
success: res => {
// 分享成功
if (res.errMsg == 'shareAppMessage:ok') {}
},
fail: res => {
// 用户取消
if (res.errMsg == 'shareAppMessage:fail cancel') {}
// 分享失败
if (res.errMsg == 'shareAppMessage:fail') {}
},
complete: res => {}
}
return data
},
}
页面引用
import share from '@/store/share.js'
export default {
mixins: [share],
data() {}
}
基本这样就能发送和分享了。
最坑的是,开发者工具一切正常,然后换到手机上,只有发送,没有分享了,百度了好久,才发现苹果手机不支持这个功能
在其他页面
that.share={
title:res.propertyname,
path:'/pages/buildDetail/buildDetail?id='+that._id,
imageUrl:(res.img.length>0?res.img[0].image:0),
desc:res.propertyname,
content:res.propertyname
}
修改成对应的参数,分享和发送的时候会自动带上
title:res.propertyname,
path:'/pages/buildDetail/buildDetail?id='+that._id,
imageUrl:(res.img.length>0?res.img[0].image:0),
desc:res.propertyname,
参数不要传错,否则一个类型不对,都会导致设置的数据不生效