uniapp全局分享好友、朋友圈

直接晒代码,三连谢谢

在utils/wxShare.js

/**
 * getCurrentPages() 获取当前页面栈的实例 - https://uniapp.dcloud.net.cn/tutorial/page.html#getcurrentpages
 * @param res
 * @var page - typeof Array [0: 首页, ..., arr.length - 1:当前页面]
 */

// 微信分享信息
let wxShare = {
  title: '', // 必须
  path: '', // 必须
  imageUrl: ''
};

// 获取页面当前栈的信息
function getCurrentPageInfo() {
  const pages = getCurrentPages();
  return `这里可以补充你小程序的名字-${ pages[pages.length - 1].route }`;
}

// 集成一个分享对象
const shareService = {
  setSharePath: function() {
    wxShare.path = getCurrentPageInfo();
    return wxShare;
  },

  // 分享给朋友
  onShareAppMessage: function(res) {
    return this.setSharePath(res);
  },

  // 分享到朋友圈
  onShareTimeline: function(res) {
    return this.setSharePath(res);
  }
};

export default shareService;

在main.js

import shareService from './utils/wxShare';

Vue.mixin({
	...shareService;
})

效果

猜你喜欢

转载自blog.csdn.net/weixin_68714627/article/details/137273172