A recent issue with vue-router beforeEach hook is an infinite loop after setting the judgment condition in beforeEach() as follows:

router.beforeEach((to, from, next) => {
  if(isLogin){
    next()
  }else{
    console.log('test')
    next('login')}})Copy the code

Results in Chrome debug:



This is how I understand the problem:

router.beforeEach((to, from, next) => {
    if(true){
        next()
    }else{
        next('login')}})Copy the code
  • Next () indicates successful routing and directly enters the to route without calling router again. BeforeEach ()
  • Next (‘login’) indicates successful route interception and redirects to login, which calls router.beforeeach () again

BeforeEach () must call next(), otherwise there is an infinite loop, next() is not the same as next(‘ XXX ‘), the former does not call the router again. BeforeEach () does!!

The official website says so (mostly the one marked in red!). :




The original link: https://segmentfault.com/a/1190000011042794