导航守卫

157 阅读1分钟

参数或查询更改不会触发进入/离开导航警卫。您可以观察$route对象以对这些更改做出反应,也可以使用beforeRouteUpdate组件内保护

全局前置守卫

const router = new VueRouter({ ... })

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

to:导航要去的路由

from: 导航要离开的路由

next(),要调用,符合条件,就放行,否则,重定向页面

router.beforeEach((to, from, next) => {
    if (to.path == '/login') return next();
    if (!window.sessionStorage.getItem('token')) {
        return next('/login')
    }
    next()
})