前置守卫和后置守卫写法

309 阅读1分钟

Vue路由 导航守卫 一.路由的前置守卫

router.beforeEach((to,from,next)=>{

    
    if (to.path === '/login') {
      next('/')
    } else {
    
      next()
    }
}

回调函数中的参数,
to:进入到那个路由去,
from:从那个路由离开,
二next:函数,决定是否展示你要看到的路由页面 next() 放行 next(false)不跳 next(路径)

二.路由后置守卫

router.afterEach((to, from) => {
  setTimeout(() => {
    NProgress.done()
  }, 500)
})
  • 只有两个参数,to:进入到哪个路由去,from:从哪个路由离。