1、背景:使用taro框架和vue3框架開發(fā)小程序,實現(xiàn)一個把pdf文件直接分享給微信好友。
一開始看taro文檔理解有偏差,導(dǎo)致分享出去的文件是個app的小程序連接。如下圖
?實現(xiàn)代碼如下:
onShareAppMessage: function () {
Taro.showShareMenu({
withShareTicket: true
// 用戶點擊右上角分享給好友,要先在分享好友這里設(shè)置menus的兩個參數(shù),才可以分享朋友圈
// menus: ['shareAppMessage', 'shareTimeline']
});
},
js:
<nut-button
shape="square"
class="bg-primary button mb-md"
style="padding: 24rpx 146rpx"
open-type="share" // 增加這個屬性才能轉(zhuǎn)發(fā)
>分享至微信聊天</nut-button
>
后續(xù)仔細查看taro文檔, Taro 文檔,發(fā)現(xiàn)這種寫法是個頁面組件,轉(zhuǎn)發(fā)出去的是個小程序,無法單獨將文件轉(zhuǎn)發(fā)。
2、實現(xiàn)文件單獨轉(zhuǎn)發(fā)給好友:微信小程序下載文件和轉(zhuǎn)發(fā)文件給好友總結(jié)_海海吶的博客-CSDN博客_小程序分享文件到微信
代碼實現(xiàn):
<nut-button
shape="square"
class="bg-primary button mb-md"
style="padding: 24rpx 146rpx"
@tap="shareFile"
:class="{ 'button-disable': isShare }"
>分享至微信聊天</nut-button
>
shareFile() {
if (this.isShare) {
return false;
}
this.isShare = true;
let that = this;
const params = {
startDateId: this.userValue.project.startDateId,
endDateId: this.userValue.project.endDateId,
fileType: 'pdf'
};
Taro.downloadFile({ // 先進行下載,接口返回后再分享文件
filePath: Taro.env.USER_DATA_PATH + `/${that.projectName}`,
url: `${config.serverUrl[getAppEnv()]}/watson/report/generateReport/${
this.userValue.project.enterpriseId
}?${queryToString(params)}`,
header: {
'content-type': 'application/json',
cookie: Taro.getStorageSync('cookieKey')
},
success(res) {
if (res.statusCode === 200) {
that.isShare = false;
Taro.shareFileMessage({
filePath: res.filePath,
fileName: that.projectName,
fail() {
showToast('分享失敗');
}
});
}
},
fail() {
showToast('分享失敗');
}
});
},
效果如下:
文章來源:http://www.zghlxwxcb.cn/news/detail-527008.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-527008.html
到了這里,關(guān)于Taro+Vue3開發(fā)微信小程序的分享好友功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!