使用vue-router的beforeEach钩子,next('/')报错

使用next()和next(false)都没问题,使用next('/index')就报错
图片描述

图片描述

阅读 19.5k
5 个回答

把堆栈打开看看。
Maximum call stack size 是不是死循环了,堆栈超出限制。


beforeEach

你这里不加条件判断,当next('/index');执行的时候beforeEach又会执行如此重复。我不太清楚你这么做的理由是什么,不过显然你不应该这样做。

if(con) {
    next('/index')
}
next()

路由1:next('/index')
路由index:next('/index')

死循环了

我猜是next('/index')时也触发了beforeach

if (to.matched.some(record => record.meta.requiresAuth)) {

 if(con) {
    next('/index')
  }

}else{

next()

}

官网上面的:
图片描述

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题