router.beforeEach超过最大调用堆栈大小(Maximum call stack size exceeded)

今天在使用router.beforeEach时,直接用next("/login")造成死循环了。

示例: 报错信息如下。

 在这里分析了一下原因:

next() 表示路由成功,直接进入to路由,不会再次调用router.beforeEach()。

next('/login') 表示路由拦截成功,路由重定向至login,会再次调用router.beforeEach()。

	} else if(JSON.parse(localStorage.getItem("userInfo")) == null){ 
    // 判断登录时间不操作 用户信息是否失效
    // next("/login")  这里直接用的话 会造成死循环。


    if(to.path == "/login"){  
      // 重新触发时会进入到这里,to.path为/login,会直接跳到login页面
      next()
    }else {   // 第一次当条件不匹配时会走这里,然后会触发钩子函数beforeEach。
      next("/login")  // 重新触发时to.path已经变为/login 就会走上面那个if判断里面。
    }

  } else {
   
   

Guess you like

Origin blog.csdn.net/weixin_48674314/article/details/118858667