vue-router自定义路由回退

我是在这篇博客vue router路由自定义后退事件的基础之上,经过自己的一些修改做出来的

// 相当于一个页面栈
window.routerList = [];
router.afterEach((to, from) => {
  window.back = function() {
      window.router.isBack = true;
      if (window.routerList.length == 0) {
          window.router.isBack = false;
          return;
      } else if (window.routerList.length == 1) {
          window.router.isBack = false;
          console.log('不能后退了!!!');
      } else {
        let backPath = window.routerList[window.routerList.length - 2].fullPath;
        window.router.push(backPath);
    }
  }
  // 如果要去的页面已经存在于页面栈中,就相当于回退
  if (!window.router.isBack) {
    var routerListLen = window.routerList.length
    if (routerListLen > 1 && window.routerList.findIndex(item => item.path == to.path)!== -1) {
      window.router.isBack = true;
    }
  }


  /*
   * 如果是后退,将当前页面从list中删除
   * 如果是前进,将当前页面添加到list中
   */
  if (window.router.isBack) {
    window.routerList.pop();
    console.log('后退', window.routerList)
  } else {
    let isExistPathIndex = window.routerList.findIndex(item => item.path == to.path)
    if (isExistPathIndex !== -1) {
      window.routerList = window.routerList.slice(0, isExistPathIndex)
    }
    window.routerList.push({
      path: to.path,
      fullPath: to.fullPath
    });
    console.log('前进', window.routerList)
  }
});
window.router = router;

在页面中,跳转路由还是使用 this.$router.push() ,页面回退改为window.back()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值