1、使用onShareAppMessage函數(shù)。在頁面的js文件中定義了 onShareAppMessage 函數(shù)時,頁面可以表示該頁面可以轉(zhuǎn)發(fā)??梢栽诤瘮?shù)中設(shè)置頁面轉(zhuǎn)發(fā)的信息。
(1)只有定義了該函數(shù),小程序右上角的菜單中才會有轉(zhuǎn)發(fā)按鈕
(2)用戶點擊轉(zhuǎn)發(fā)按鈕的時候回調(diào)用該函數(shù)
(3)該函數(shù)內(nèi)需要 return 一個 Object,Object中包含轉(zhuǎn)發(fā)的信息(可自定義轉(zhuǎn)發(fā)的內(nèi)容)
2、一個是頁面中具有屬性open-type且其值為share的button。(注:必須是button組件,其他組件中設(shè)置 open-type=“share” 無效)
即:<button data-name="shareBtn" open-type="share">轉(zhuǎn)發(fā)</button>
注意!!! onShareAppMessage是頁面的方法, 組件內(nèi)不會觸發(fā)。
小程序中分享功能需要在button組件中添加open-type:“share”,當點擊button的時候就會調(diào)用onShareAppMessage函數(shù)。如果需要將按鈕的參數(shù)傳入給函數(shù),使用? ? ? ?:data-屬性=值
html:
<view class="activity-box" v-for="(item,index) in activity" >
<view class="activity-title flex between center-v">
<view class="title text-ellipsis">
{{item.theme}}
</view>
<button type="default" open-type="share" :data-item='item'>
<image :src="getStaticIconPath('icon-zhuanfa.png')" mode="" class="_inco"></image>
</button>
</view>
</view>
js:
//分享函數(shù)
onShareAppMessage(res) {
console.log(res,'我是分享'); //打印出來的就是:data-item='item'里的'item'值
//也就是循環(huán)里的item值,循環(huán)里的每一項
const that = this;
let data = res.target.dataset.item; //取到傳入的參數(shù)
let shareobj = {
title:'',//分享的標題 默認是小程序的名稱
path:"",//好友點擊分享之后跳轉(zhuǎn)的頁面 默認是當前頁面,必須是以‘/’開頭的完整路徑
imageUrl: "",//自定義圖片路徑,可以是本地文件路徑、代碼包文件路徑或者網(wǎng)絡(luò)圖片路徑,支持PNG及JPG,不傳入 imageUrl 則使用默認截圖。顯示圖片長寬比是 5:4
}
//來自頁面按鈕內(nèi)的分享
if(res.from === 'button'){
shareobj.title="", //你要分享的標題
shareobj.path="", //你要跳轉(zhuǎn)的頁面路徑
shareobj.imageUrl="",//你要分享的圖片
}
return shareobj//最后一定要返回對象
},
效果圖如下: (console.log(res,'我是分享'))文章來源:http://www.zghlxwxcb.cn/news/detail-516284.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-516284.html
到了這里,關(guān)于uniapp小程序分享功能onShareAppMessage函數(shù)傳參數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!