vue 路由守卫

1.登录成功后

.then(res => {
// console.log(res);
// 检验成功 设置登录状态并且跳转到/
localStorage.setItem("ele_login", true);
this.$router.push("/");

}

router.js里面

// 路由守卫
router.beforeEach((to, from, next) => {
const isLogin = localStorage.ele_login ? true : false;
if (to.path == '/login') {
next();
} else {
// 是否在登录状态下
isLogin ? next() : next('/login');
}
});

原文地址:https://www.cnblogs.com/xzhce/p/13690248.html