解决vue2.0路由跳转未匹配相应用路由避免出现空白页面
2019-08-16 本文已影响0人
落花夕拾
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();
}
});