H女王范儿 2020-04-22
话不多说,上代码!!!
vuex中:
const state = {
//先去localStorage中获取数据
userInfo: JSON.parse(localStorage.getItem("userInfo")) || {},
}
const mutations = {
setuserInfo(state, v) {
//将传递的数据先保存到localStorage中
localStorage.setItem(‘userInfo‘, JSON.stringify(v));
// 之后才是修改state中的状态
state.userInfo = v;
},
}组件中逻辑:在登录成功之后提交mutations,更改用户信息
if(res.data.code === 0){
this.user = res.data.data
this.$store.commit("setuserInfo", this.user);
this.$Message[‘success‘]({
background: true,
content: ‘登陆成功,即将跳转...‘
});
this.$router.push(‘/chat‘)
}组件中逻辑:退出也要更新信息(注意不要写成null,要写成‘‘)
this.$store.commit(‘setuserInfo‘,‘‘);//更新userInfo