微信小程序怎么實(shí)現(xiàn)分享?在小程序功能開(kāi)發(fā)中,多個(gè)頁(yè)面都會(huì)涉及使用分享,需要我們把分享功能,單獨(dú)寫(xiě)在一個(gè)公用的文件中。util.js 文件中。
//分享功能
const shareEvent = (option, obj) => {
let shareObj = {
title: obj.title,
path: obj.path,
imgUrl: obj.imgUrl,
success(res){
// 轉(zhuǎn)發(fā)成功之后的回調(diào)
if (res.errMsg == 'shareAppMessage:ok') {}
},
fail(res){
// 轉(zhuǎn)發(fā)失敗之后的回調(diào)
if (res.errMsg == 'shareAppMessage:fail cancel') {
// 用戶(hù)取消轉(zhuǎn)發(fā)
} else if (res.errMsg == 'shareAppMessage:fail') {
// 轉(zhuǎn)發(fā)失敗,其中 detail message 為詳細(xì)失敗信息
}
},
complete(){
// 轉(zhuǎn)發(fā)結(jié)束之后的回調(diào)(轉(zhuǎn)發(fā)成不成功都會(huì)執(zhí)行)
}
};
if (option.from === 'button') {
// 來(lái)自頁(yè)面內(nèi)轉(zhuǎn)發(fā)按鈕
console.log(option.target)
}
return shareObj;
}
在使用分享的頁(yè)面中引入util.js
const util = require('./utils/util.js');
/**
用戶(hù)點(diǎn)擊右上角分享
*/
onShareAppMessage: function(option){
console.log(option);
let obj = {
title: '我的老窩',
path: 'pages/index/index',
imageUrl: ''
};
return util.shareEvent(option, obj);
}
備注:記得要在調(diào)用的時(shí)候使用,return。
教程來(lái)源:PHP中文網(wǎng)
榮勝網(wǎng)絡(luò)官網(wǎng)有海量移動(dòng)建站的精品案例,為您提供網(wǎng)絡(luò)營(yíng)銷(xiāo)一站式服務(wù),榮勝網(wǎng)絡(luò)官網(wǎng)鏈接 http://m.millpower.com.cn/
