解決vuex數(shù)據(jù)丟失問題
數(shù)據(jù)丟失的原因
vuex存儲的數(shù)據(jù)只是在頁面中,相當(dāng)于全局變量,頁面刷新的時候vuex里的數(shù)據(jù)會重新初始化,導(dǎo)致數(shù)據(jù)丟失。
因為vuex里的數(shù)據(jù)是保存在運行內(nèi)存中的,當(dāng)頁面刷新時,頁面會重新加載vue實例,vuex里面的數(shù)據(jù)就會被重新賦值。
方法1:使用第三方庫 vuex-persistedstate
npm install --save vuex-persistedstate
01 store / index.js 之 localStorage
- 注意點: vuex-persistedstate默認存儲在 localStorage之中,基本上不需要配置什么
import createPersistedState from "vuex-persistedstate" const store =newVuex.Store({ state: { cartList: [], }, mutations: {}, actions: {}, // 當(dāng)state中的值發(fā)生改變,此時localStorage中的vuex的值會同步把state中的所有值存儲起來,當(dāng)頁面刷 新的時候,state的值會從localStorage自動獲取vuex的value值,賦值到state中 plugins: [createPersistedState()] })
02 store / index.js 之 sessionStorage
import createPersistedState from "vuex-persistedstate" const store = new Vuex.Store({ state: {}, mutations: {}, actions: {}, plugins: [createPersistedState({ storage:window.sessionStorage // 同localStorage相同,只是將vuex的所有值存儲到sessionStorage中 })] })
03 store / index.js 之 使用vuex-persistedstate指定需要持久化的state
import createPersistedState from "vuex-persistedstate" const store = newVuex.Store({ ?state: { count: 0 }, ?mutations: {}, ?actions: {}, ?plugins: [createPersistedState({ ? storage:window.sessionStorage, ? reducer(val) ?{ ? ? ? ? ?// 此時,當(dāng)count發(fā)生改變的時候,就會調(diào)用此函數(shù),并且val的值為當(dāng)前state對象,return的值為當(dāng)前本地存儲的value值(本地存儲的key值為vuex) ? ? ? ? ?return { ? ? ? ? ? ? ?count: val.count, ? ?changeCount: 'aaa' ? ? ? ? ?} ? ? ?} ?})] })
方法2 把state的數(shù)據(jù)先緩存到localStorage之中,頁面刷新的時候,拿到數(shù)據(jù)寫入vuex
store / index.js
import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex); export default new Vuex.Store({ state: { orderList: [], menuList: [] }, mutations: { orderList(s, d) { s.orderList= d; window.localStorage.setItem("list",jsON.stringify(s.orderList)) }, menuList(s, d) { s.menuList = d; window.localStorage.setItem("list",jsON.stringify(s.menuList)) }, } })
頁面刷新的時候
通過監(jiān)聽beforeunload事件來進行數(shù)據(jù)的localStorage存儲,beforeunload事件在頁面刷新時進行觸發(fā),具體做法是在App.vue的created()周期函數(shù)中下如下代碼
if (window.localStorage.getItem("list") ) { this.$store.replaceState(Object.assign({}, this.$store.state,JSON.parse(window.localStorage.getItem("list")))) } window.addEventListener("beforeunload",()=>{ window.localStorage.setItem("list",JSON.stringify(this.$store.state)) })
到此這篇關(guān)于解決vuex數(shù)據(jù)丟失問題的文章就介紹到這了,更多相關(guān)vuex 數(shù)據(jù)丟失內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vuex刷新之后數(shù)據(jù)丟失,數(shù)據(jù)持久化,vuex-persistedstate問題
- vuex頁面刷新數(shù)據(jù)丟失問題的四種解決方式
- 解決vue頁面刷新vuex中state數(shù)據(jù)丟失的問題
- vuex結(jié)合session存儲數(shù)據(jù)解決頁面刷新數(shù)據(jù)丟失問題
- 關(guān)于vuex強刷數(shù)據(jù)丟失問題解析
- vuex頁面刷新導(dǎo)致數(shù)據(jù)丟失的解決方案
- vuex刷新后數(shù)據(jù)丟失的解決方法
- vuex存儲復(fù)雜參數(shù)(如對象數(shù)組等)刷新數(shù)據(jù)丟失的解決方法
- vuex頁面刷新后數(shù)據(jù)丟失的方法
相關(guān)文章
vue3使用element-plus再次封裝table組件的基本步驟
這篇文章主要介紹了vue3使用element-plus再次封裝table組件的基本步驟,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-03-03npm如何更新VUE package.json文件中依賴的包版本
這篇文章主要介紹了npm如何更新VUE package.json文件中依賴的包版本問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06vue?動態(tài)路由component?傳遞變量報錯問題解決
這篇文章主要為大家介紹了vue?動態(tài)路由component?傳遞變量報錯問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05