微信小程序分享功能的实现方法有两种:
1. index.js中定义 onShareAppMessage,其实是小程序本来就有的方法。只是实现一下。
便可在小程序右上角选择分享该页面
pages\index\index.js
onShareAppMessage:function(){
return{
title:'测试分享传数据',
desc:'分享的内容',
path:'/pages/show/show?id=1212'
}
}
2.
自定义按钮实现分享,在page中添加一个带有open-type='share'
的button标签(<button open-type='share'><\/button>
)。点击该按钮后,即会自动触发已经在page.js中定义好的onShareAppMessage方法,实现分享功能。
<button open-type='share'>分享</button>
pages\index\index.wxml
<!--index.wxml-->
<button open-type="share">分享</button>
获取分享的参数
如上例,path属性指向的是show页面,并附带id=1212的参数。我们只需在show.js的onLoad函数中,通过options查看传递过来的参数:
扫描二维码关注公众号,回复:
12840125 查看本文章
pages\show\show.js
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
console.log('111')
console.log(options)
console.log('222')
},