微信小程序如何实现点击按钮分享和自定义标题、图片?
在页面文件 wxml 文件中添加 button 组件 (必须使用 button 组件才可以),必须添加 open-type 属性且值为 share
<button open-type="share" class='btn-share' hover-class='none'>邀请好友</button>
在对应的 js 文件 Page() 中添加 onShareAppMessage 函数。如果不需要自定义分享的标题、图片或是 page 路径,则不需要返回任何内容。默认分享页面为当前页面,标题为当前页面的标题,图片为当前页面头部截图。
onShareAppMessage () {
}
如果需要自定义分享标题、图片、分享链接,则需要返回一个对象
onShareAppMessage: function (res) {
// 可以通过判断 res.from 判断触发分享的方式。
return {
title: '你的好友喊你来领钱啦...',
path: '/pages/invite/index',
imageUrl: 'https://nideshop-static.childsay.com/qrcode.jpg'
}
}
注意 path 必须以 /
开头。分享图片尺寸推荐宽高比为5:4,如500*400。