//在Home里面写
if (this.current == "laowang" && this.password == "laowang666") {
//若账号或密码正确,则在缓存里面存一个isLogin值为1
localStorage.setItem("isLogin", "1");
this.$router.push("/Dashboard");
}
//在router--index.js里面写以下内容判断是否登录
const router = new VueRouter({ routes });
router.beforeEach((to, from, next) => {
//若能获取缓存里面的isLogin则说明已经登陆了,可以成功跳转
if (localStorage.getItem("isLogin") == "1") {
next();
} else {
if (to.name == "Home") {
next();
} else {
next({ name: "Home" });
alert("请先登录");
next();
}
}
});
export default router;
vue路由守卫
最新推荐文章于 2024-04-26 10:02:08 发布