On the problem of vue-router interceptor

my requirement is that the user opens the page, determines whether the index page (/ default is index) has login status, and continues without jumping login,. Now the problem is that I can"t block it by opening the index connector by default, and next directly. Then I click on other routes and intercept them when they return to the index page. What"s the problem? why can"t I intercept

in the first place?
Jul.22,2021

you can use beforeEach as a routing guard

router.beforeEach((to, from, next) => {
  // to and from are both route objects
  let login = sessionStorage['token'] || null
  let path = to.path
  if (path === '/login') {
    next()
    return
  }
  //  to.meta.requireAuth 
  let requireAuth = to.meta.requireAuth
  if (requireAuth) { // 
    if (!login) { // 
      next({
        path: '/login'
      })
    } else { // 
      next()
    }
  } else { // 
    next()
  }
})
Menu