Vue实现登录保存token并校验实现保存登录状态的操作代码
作者:李长渊哦
这篇文章主要介绍了Vue实现登录保存token并校验实现保存登录状态,本文通过示例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧
一、登录vue
<script>
import request from "@/axios/baseURL";
import router from "@/router";
// 接口数据初始化
const FORM_DATA = {
userName: "",
password: "",
};
export default {
data() {
return {
FORM_DATA,
};
},
created() {
console.log("登录界面");
},
methods: {
login() {
request.post("/systemUser/login", this.FORM_DATA).then((res) => {
var code = res.data.code;
var message = res.data.message;
this.$message(message);
if (code == 0) {
localStorage.setItem("token", res.data.data.token);
router.push("/library");
}
console.log(res);
});
},
},
};
</script>二、路由index
// 导航守卫
router.beforeEach((to, from, next) => {
const token = localStorage.getItem('token');
const outerPaths = ['/homePage']; // 当前 path 不需要登录也可以进入系统,但是只能操作当前页面
if (!token && !outerPaths.includes(to.path)) {
next('/homePage');
} else {
// if (to.path == "/auth") {
// document.title = to.meta.title // 进入这个页面会被更改页面标题
// } else document.title = 'CPS流量变现后台管理系统'
next();
}
});
到此这篇关于Vue实现登录保存token并校验实现保存登录状态的文章就介绍到这了,更多相关Vue登录保存token内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
