vuex页面刷新数据丢失问题

编程入门 行业动态 更新时间:2024-10-11 23:16:19

vuex页面刷新<a href=https://www.elefans.com/category/jswz/34/1724024.html style=数据丢失问题"/>

vuex页面刷新数据丢失问题

三种方式解决问题: 

1、保存在localStorage/sesstionStorage/cookie 中

import Vue from 'vue'
import Vuex from 'vuex'Vue.use(Vuex)const actions = {}
const mutations = {handleUserName: (state, user_name) => {state.user_name = user_name// 把登录的用户的名保存到localStorage中,防止页面刷新,导致vuex重新启动,用户名就成为初始值(初始值为空)的情况localStorage.setItem('user_name', user_name)}
}
const state = {user_name: '' || localStorage.getItem('user_name')
}
// getters 只会依赖 state 中的成员去更新
const getters = {userName: (state) => state.user_name
}const store = new Vuex.Store({actions,mutations,state,getters
})
export { store }

2、在vuex中每次调用这个接口

import Vue from 'vue'
import Vuex from 'vuex'
import { setToken } from '@/util/jscookie.js'
Vue.use(Vuex)const store = new Vuex.Store({state:{Authorization: ""},mutations:{changeLogin(state,user){state.Authorization = user.Authorization;setToken(state.Authorization);}}
})export default store

更多推荐

vuex页面刷新数据丢失问题

本文发布于:2023-11-17 02:36:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638167.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据丢失   页面   vuex

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!