小程序全局分享onShareAppMessage

自建博客文章链接:http://www.heblogs.cn/articleDetails/60fa585ab69f2b0a1af648eb

在app.js内 写一个方法 用wx.onAppRoute监听路由变化 每当路由变化时
给当前页面重新写入一个onShareAppMessage分享配置 再将该方法放在app.js内的onLaunch中去执行 这样就能全局分享啦
让每个页面分享的标题 内容 图片都一样了 如果你想个别页面不需要重写 你可以看看我注释的地方

  onLaunch(){
    
    
    this.onShareAppMessage()
  },
  onShareAppMessage(){
    
    
    wx.onAppRoute(() =>{
    
    
      console.log('当前页面路由发生变化 触发该事件onShareAppMessage')
      const pages = getCurrentPages() //获取加载的页面
      const view = pages[pages.length - 1] //获取当前页面的对象
      if(!view)  return false  //如果不存在页面对象 则返回
      // 若想给个别页面做特殊处理 可以给特殊页面加isOverShare为true 就不会重写了
      // const data = view.data
      // if (!data.isOverShare) {
    
    
        // data.isOverShare = true
        view.onShareAppMessage = () => {
    
     //重写分享配置
          return {
    
    
            title: '微信小程序全局分享',
            path: "/pages/home/index", //若无path 默认跳转分享页
            imageUrl:'/image/onshowMessage.png' //若无imageUrl 截图当前页面
          }
        }
      // }
    })
  },

猜你喜欢

转载自blog.csdn.net/weixin_45815859/article/details/111591369