标签:信息保存 获取 保存 信息 inf 体验 用户信息 null 修改
话不多说,上代码!!!
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
vue 中使用vuex和localStorage保存登录状态
标签:信息保存 获取 保存 信息 inf 体验 用户信息 null 修改
原文地址:https://www.cnblogs.com/yaya-003/p/12750819.html