router.beforeEach((to, from, next) => {
if (to.matched.length ===0) { //如果未匹配到路由
from.name ? next({ name:from.name }) : next('/'); //如果上级也未匹配到路由则跳转登录页面,如果上级能匹配到则转上级路由
} else {
next(); //如果匹配到正确跳转
}
});
router.beforeEach((to, from, next) => {
let token = sessionStorage.getItem('token');
// let needAuth = to.matched.some(record => record.meta.requiresAuth);
if(!token && to.path !='/login'){
sessionStorage.removeItem('token');
next({path: "/login"});
}else {
next();
}
});
网友评论