How to return to the home page from the shared secondary page of the Mini Program

Solve the problem that after the inner page is shared, the user cannot return to the home page through the shared page

The essence is to set a variable isshare to determine whether it is entered in the sharing page

data setting

Page({
   data: {

        isShare : false, //标识是否是从分享进入的小程序
        /*
        *下面写你自己需要的其他变量
        */
   },

   onLoad(options) {
    //如果是从分享页面进入,则修改标识
     if(options.isshare == 1){
         this.setData({
           isShare : true
          })
      }
   },

/* 分享给朋友 */
   onShareAppMessage() {
    return {
      title: '这里写上分享的文案!',
      path: 'pages/report/report?isshare=1' //这里写当前页面的路径,后面带上参数isshare=1
    }
  },

/* 分享至朋友圈 */
  onShareTimeline(){
    return{
      title: '这里写上分享的文案!',
      path: 'pages/report/report?isshare=1'
    }
  },

/* 
  *返回上一级页面
  *因为分享出去的页面,没有带上一级页面,所以不能直接返回 
*/
  BackPage() {
    wx.navigateBack({
      delta: 1
     })
  },

/* 分享页面的返回按钮,实际上是关闭其他页面,直接打开首页 */
  toHome() {
    wx.reLaunch({
      url: '/pages/index/index'
    })
  },

/* 其他代码 */

})

wxml

<image bindtap="{
    
    {isShare? 'toHome':'BackPage'}}" class='d-back-home' src='http://cdn.xcx.pemarket.com.cn/icon-Return%20to%20the%20home%20page.png' lazy-load></image>

WXSS

/* 返回按钮,固定定位,悬浮 */
.d-back-home {
  position: fixed;
  width: 96rpx;
  height: 96rpx;
  right: 30rpx;
  bottom: 166rpx;
  z-index: 10000;
}

Explanation: In the actual development, I made some packaging for the back button. The above code is just to illustrate the problem, not the actual code of the rendering.

Guess you like

Origin blog.csdn.net/laya1211/article/details/129613417