export default { state:{ token:false, status:false, userInfo:{} }, mutations:{ // 登陆成功后渲染数据 login(state,userInfo){ state.userInfo = userInfo state.token = userInfo.token state.status = userInfo.status window.sessionStorage.setItem('userInfo',JSON.stringify(userInfo)) window.sessionStorage.setItem('token',JSON.stringify(userInfo.token)) }, // 初始化存储 initUser(state) { let userinfo = window.sessionStorage.getItem('userInfo') let userInfo = JSON.parse(userinfo) if (userInfo) { state.userInfo = userInfo state.token = userInfo.token state.status = userInfo.status } }, // 退出登录删除数据 loginOut(state) { state.userInfo = {} state.token= false state.status= false, window.sessionStorage.removeItem('userInfo') window.sessionStorage.removeItem('token') }, }, getters:{}, actions:{ }, }
Login.vue 登陆成功后渲染数据
this.$axios({ method: "post", url: "/login", data: { username: this.username, password: this.password, }, }).then((result) => { console.log(result); if ( this.username === result.data.data.username || this.password === result.data.data.password ) { Toast("登陆成功!"); this.login(result.data.data); return this.$router.push("/Myfile"); } else { Toast("账号或密码不正确,请重新输入"); this.pshow = true; return (this.show = false); } })
app.vue进行初始化
myfile.vue 退出登录删除数据
this.$axios({ url: "/logout", method: "post", data: {}, headers: { token: true // token:JSON.parse(sessionStorage.getItem('token')) }, }).then((v) => { console.log('1111111111222222'); console.log(v); }); this.$router.push("/Login"); setTimeout(() => { this.loginOut(); }, 100); }) .catch(() => { // });