Home > Article > WeChat Applet > How to implement sharing function in mini program (code example)
The content of this article is about how to implement the sharing function (code example) in the mini program. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Because multiple pages in the mini program use sharing, we need to write the sharing function separately in a common file. util.js file.
//Sharing function
const shareEvent = (option, obj) => { let shareObj = { title: obj.title, path: obj.path, imgUrl: obj.imgUrl, success(res){ // 转发成功之后的回调 if (res.errMsg == 'shareAppMessage:ok') {} }, fail(res){ // 转发失败之后的回调 if (res.errMsg == 'shareAppMessage:fail cancel') { // 用户取消转发 } else if (res.errMsg == 'shareAppMessage:fail') { // 转发失败,其中 detail message 为详细失败信息 } }, complete(){ // 转发结束之后的回调(转发成不成功都会执行) } }; if (option.from === 'button') { // 来自页面内转发按钮 console.log(option.target) } return shareObj; }
Introduce util.js into the page that uses sharing
const util = require('./utils/util.js');
/** 用户点击右上角分享 */ onShareAppMessage: function(option){ console.log(option); let obj = { title: '我的老窝', path: 'pages/index/index', imageUrl: '' }; return util.shareEvent(option, obj); }
Note: Remember to use return when calling.
Related recommendations:
Example sharing of WeChat applet implementation of process progress functionExample of sharing code for friends circle in applet development
How to get the return value of sharing success or failure from the sharing function
The above is the detailed content of How to implement sharing function in mini program (code example). For more information, please follow other related articles on the PHP Chinese website!