Home > WeChat Applet > Mini Program Development > How to implement sharing function in mini program (code example)

How to implement sharing function in mini program (code example)

不言
Release: 2018-09-01 10:58:35
Original
11375 people have browsed it

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;
}
Copy after login

Introduce util.js into the page that uses sharing

const util = require('./utils/util.js');
Copy after login
/**
用户点击右上角分享
*/
onShareAppMessage: function(option){
    console.log(option);
    let obj = {
      title: '我的老窝',
      path: 'pages/index/index',
      imageUrl: ''
    };
    return util.shareEvent(option, obj);
  }
Copy after login

Note: Remember to use return when calling.

Related recommendations:

Example sharing of WeChat applet implementation of process progress function

Example 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template