美文网首页
解决vue2.0路由跳转未匹配相应用路由避免出现空白页面

解决vue2.0路由跳转未匹配相应用路由避免出现空白页面

作者: 落花夕拾 | 来源:发表于2019-08-16 14:21 被阅读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();
  }
});

相关文章

网友评论

      本文标题:解决vue2.0路由跳转未匹配相应用路由避免出现空白页面

      本文链接:https://www.haomeiwen.com/subject/gznfsctx.html