一、定义
路由拦截是路由在发生变化时需要进行的拦截处理,比如跳转某个页面内要判断是否有登录等
二、用法
beforeEach函数有是三个参数:
to:router即将进入的路由对象
from:当前导航即将离开的路由
next:Function,进行管道中的一个钩子,如果执行完了,则导航的状态就是confirmed(确认的);否则为false,终止导航
afterEach函数不用传next()函数
router.beforeEach( function(to , from , next) {
next()
})
例如:
router.beforeEach( function( to , from , next) {
if(!localStorage.getItem("username")){
if( to.path !== '/login'){
next('/login')
}
};
next()
})