导航前置守卫
vue-router
提供的导航守卫主要用来通过跳转或取消的方式守卫导航,说白了,就是控制路由到底能不能跳转,应该如何跳转
- 全局前置守卫会拦截每个路由规则,从而对每个路由进行访问权限的控制
- 全局的前置守卫是路由跳转前进行触发的
- 我们可以使用
router.beforeEach
注册一个全局前置守卫
router.beforeEach((to, from, next) => {
// ${to and from are Route Object,next() must be called to resolve the hook}
// console.log(to);
next()
})
- 守卫方法的 3 个形参
to
: Route: 即将要进入的目标 路由对象from:
Route: 当前导航正要离开的路由next
: Function: 一定要调用该方法来resolve
这个钩子,否则跳转会中止,无法进行下一步了