The uniapp applet sharing function onShareAppMessage function passes parameters

1. Use the onShareAppMessage function. When the onShareAppMessage function is defined in the js file of the page, the page can indicate that the page can be forwarded. You can set the page forwarding information in the function.
(1) Only when this function is defined, there will be a forward button in the menu in the upper right corner of the applet
(2) When the user clicks the forward button, this function will be called back
(3) This function needs to return an Object, which contains the forwarded Information (you can customize the forwarded content)

2. One is a button with an attribute open-type and its value is share in the page. (Note: It must be a button component, setting open-type="share" in other components is invalid)

Right now:<button data-name="shareBtn" open-type="share">转发</button>

Note!!! onShareAppMessage is a method of the page, and will not be triggered in the component.

The sharing function in the applet needs to add open-type: "share" to the button component, and the onShareAppMessage function will be called when the button is clicked. If you need to pass the parameters of the button to the function, use       :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:

//分享函数
		onShareAppMessage(res) {
			console.log(res,'我是分享');  //打印出来的就是:data-item='item'里的'item'值
                                         //也就是循环里的item值,循环里的每一项

            const that = this;
			let data = res.target.dataset.item; //取到传入的参数

            let shareobj = {
				title:'',//分享的标题    默认是小程序的名称
				path:"",//好友点击分享之后跳转的页面   默认是当前页面,必须是以‘/’开头的完整路径
				imageUrl: "",//自定义图片路径,可以是本地文件路径、代码包文件路径或者网络图片路径,支持PNG及JPG,不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
			}

            //来自页面按钮内的分享
			if(res.from === 'button'){
				shareobj.title="",   //你要分享的标题
                shareobj.path="",    //你要跳转的页面路径
                shareobj.imageUrl="",//你要分享的图片
			}

			return shareobj//最后一定要返回对象

		},

The rendering is as follows: (console.log(res,'I am sharing'))

Guess you like

Origin blog.csdn.net/weixin_64103049/article/details/128255478