(简单实用)vue路由登录拦截(导航守卫拦截关键代码逻辑)

导航守卫拦截代码逻辑

router.beforeEach((to, from, next) => {
  let islogin = localStorage.getItem("islogin");
  islogin = Boolean(Number(islogin));
 
  if(to.path == "/login"){
    if(islogin){
      next("/table");
    }else{
      next();
    }
  }else{
    // requireAuth:可以在路由元信息指定哪些页面需要登录权限
    if(to.meta.requireAuth && islogin) {
      next();
    }else{
      next("/login");
    }
  }
})

猜你喜欢

转载自blog.csdn.net/qq_59747594/article/details/132392612