uniapp开发微信小程序-分享当前页面到好友或朋友圈

808 阅读1分钟

一、前置条件

  • 定义一个按钮,设置open-type="share"
<button class="forward" open-type="share" >转发</button>

二、事件触发

  • 点击按钮会触发onShareAppMessage事件
  • res中包含res.from及当前行等信息,通过res.from === 'button'条件判定来自分享按钮
  • 必须有返回值,内含标题title跳转路径pathmpId用于配置微信小程序的AppId
  • 跳转路径可以携带参数
  • mpId配置后其他用户才有权限访问

注意:onShareAppMessage事件与onLoad、methods、components等同级

onShareAppMessage(res) {
	console.log('列表信息',res);
	// console.log('列表信息',res.target.dataset.eventParams.ele.title);
	if (res.from === 'button') { // 来自页面内分享按钮
		console.log(res.target)
	}
	return {
		title: res.target.dataset.eventParams.ele.title, //分享的名称
		path: 'pages/interpretAndDownload/interpretAndDownload?title=模版下载',
		mpId: appid //此处配置微信小程序的AppId
	}
},