小程序中怎么实现分享功能

这篇文章主要介绍了小程序中怎么实现分享功能,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下面让小编带着大家一起了解一下。

util.js 文件中。

//分享功能

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;
}

在使用分享的页面中引入util.js

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);
  }

备注:记得要在调用的时候使用,return。

感谢你能够认真阅读完这篇文章,希望小编分享小程序中怎么实现分享功能内容对大家有帮助,同时也希望大家多多支持亿速云,关注行业资讯频道,遇到问题就找亿速云,详细的解决方法等着你来学习!