小程序 顶部返回按钮

 路由的实现方案:

 

目录

一、switchTab--跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面。

二、reLaunch--关闭所有页面,打开到应用内的某个页面。

三、redirectTo-关闭当前页面,跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面。

四、navigateTo--保留当前页面,跳转到应用内的某个页面。但是不能跳到 tabbar 页面。

五、navigateBack--关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages 获取当前的页面栈,决定需要返回几层。

 


一、switchTab--跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面。

gotoPage1(){// switchTab-跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面
      wx.switchTab({
        url: '../../../index/index',
        success: function(res){
          // success
          wx.showToast({
            title:'成功111',
            icon:'none'
          })
        },
        fail: function() {
          // fail
          wx.showToast({
            title:'失败',
            icon:'none'
          })
        },
        complete: function() {
          // complete
          wx.showToast({
            title:'好好学习,天天向上!',
            icon:'none'
          })
        }
      })
    },

需要在json里配置好tabBar页面(tabBar至少有2个页面):

  "tabBar": {
    "list": [{
      "pagePath": "pages/index/index",
      "text": "首页"
    },{
      "pagePath": "pages/other/index",
      "text": "其他"
    }]
  },

 


二、reLaunch--关闭所有页面,打开到应用内的某个页面。

gotoPage2(){// reLaunch-关闭所有页面,打开到应用内的某个页面
      wx.reLaunch({
        url: '../../products/ylS1908002/index',
        success: (res) => {
          // success
          wx.showToast({
            title:'成功进入222',
            icon:'none'
          })
        },
        fail: () => {
          wx.showToast({
            title:'失败',
            icon:'none'
          })
        },
        complete: () => {
          wx.showToast({
            title:'好好学习,天天向上!',
            icon:'none'
          })
        }
      });
    },

 


三、redirectTo-关闭当前页面,跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面。

gotoPage3(){// redirectTo-关闭当前页面,跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面。
      wx.redirectTo({
        url: '../../products/ylS1908003/index',
        success: function(res){
          // success
          wx.showToast({
            title:'成功进入333',
            icon:'none'
          })
        },
        fail: function() {
          // fail
          wx.showToast({
            title:'失败',
            icon:'none'
          })
        },
        complete: function() {
          // complete
          wx.showToast({
            title:'好好学习,天天向上!',
            icon:'none'
          })
        }
      })
    },

 


四、navigateTo--保留当前页面,跳转到应用内的某个页面。但是不能跳到 tabbar 页面。

gotoPage4(){// navigateTo-保留当前页面,跳转到应用内的某个页面。但是不能跳到 tabbar 页面。
      wx.navigateTo({
        url: '../../products/ylS1908005/index',
        success: function(res){
          // success
          wx.showToast({
            title:'成功进入444',
            icon:'none'
          })
        },
        fail: function() {
          // fail
          wx.showToast({
            title:'失败',
            icon:'none'
          })
        },
        complete: function() {
          // complete
          wx.showToast({
            title:'好好学习,天天向上!',
            icon:'none'
          })
        }
      })
    },

 


五、navigateBack--关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages 获取当前的页面栈,决定需要返回几层。

    data:{
        pageNum:0
    }

    onLoad: function (options) {
      // 获取总页面数
      var pageNum = getCurrentPages().length;// 是2
      this.setData({// 设置前一个页面数
        "pageNum" : pageNum-2
      });
    },

    gotoPage5(){// navigateBack-关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages 获取当前的页面栈,决定需要返回几层。
      wx.navigateBack({
        delta: this.data.pageNum, // 回退前 delta(默认为1) 页面
        success: function(res){
          // success
          wx.showToast({
            title:"成功返回上一页",
            icon: 'none'
          })
        },
        fail: function() {
          // fail
          wx.showToast({
            title:'失败',
            icon:'none'
          })
        },
        complete: function() {
          // complete
          wx.showToast({
            title:'好好学习,天天向上!',
            icon:'none'
          })
        }
      })
    }

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值