vuex刷新頁(yè)面后如何解決丟失store的數(shù)據(jù)問(wèn)題
vuex刷新頁(yè)面后丟失store的數(shù)據(jù)
提出問(wèn)題
最近遇到個(gè)問(wèn)題,vue中vuex中的store數(shù)據(jù),會(huì)在頁(yè)面刷新后初始化,為了解決這一問(wèn)題決定將store里面數(shù)據(jù)在頁(yè)面刷新前保存到sessionStorage,至于為何用他,因?yàn)轫?yè)面關(guān)閉后他會(huì)被清空,localStorage則會(huì)一直存在,cookie又太小,因此sessionStorage最合適。
解決方案
在app.vue的created方法中讀取sessionstorage中的數(shù)據(jù)存儲(chǔ)在store中,此時(shí)用vuex.store的replaceState方法,替換store的根狀態(tài)
在beforeunload方法中將store.state存儲(chǔ)到sessionstorage中。
export default { ? name: 'app', ? created () { ? ? // 在頁(yè)面加載時(shí)讀取sessionStorage ? ? if (sessionStorage.getItem('store')) { ? ? ? this.$store.replaceState(Object.assign({}, this.$store.state, JSON.parse(sessionStorage.getItem('store')))) ? ? } ? ? // 在頁(yè)面刷新時(shí)將store保存到sessionStorage里 ? ? window.addEventListener('beforeunload', () => { ? ? ? sessionStorage.setItem('store', JSON.stringify(this.$store.state)) ? ? }) ? } }
vuex刷新頁(yè)面數(shù)據(jù)丟失(數(shù)據(jù)持久化)
前提:
為什么要讓vuex數(shù)據(jù)持久化:
在使用vuex的時(shí)候,會(huì)發(fā)現(xiàn)刷新頁(yè)面之后state中存儲(chǔ)的數(shù)據(jù)會(huì)被重置,因?yàn)樗⑿聻g覽器的時(shí)候會(huì)導(dǎo)致整個(gè)頁(yè)面重新加載,vuex的state也會(huì)全部重新加載,所以為了防止這類情況的發(fā)生,我們會(huì)將vuex中的數(shù)據(jù)進(jìn)行本地存儲(chǔ),防止頁(yè)面刷新丟失vuex中的數(shù)據(jù)。
存儲(chǔ)到localStorage中
首先安裝vuex-persistedstate
npm install vuex-persistedstate -S
vuex-persistedstate默認(rèn)存儲(chǔ)到localStorage,使用如下:
在store文件夾下面的index.js中引入
import Vue from 'vue' import Vuex from 'vuex' import createPersistedState from "vuex-persistedstate" Vue.use(Vuex) const store = new Vuex.Store({ ? ? plugins: [createPersistedState()], })
存儲(chǔ)到sessionStorage中
使用vuex-persistedstate存儲(chǔ)到sessionStorage,如下:
import Vue from 'vue' import Vuex from 'vuex' import createPersistedState from "vuex-persistedstate" Vue.use(Vuex) const store = new Vuex.Store({ ? ? plugins: [createPersistedState({ ? ? ? ? storage: window.sessionStorage ? ? })], })
指定數(shù)據(jù)持久化
使用vuex-persistedstate指定需要持久化的state數(shù)據(jù),如下:
import Vue from 'vue' import Vuex from 'vuex' import createPersistedState from "vuex-persistedstate" Vue.use(Vuex) const store = new Vuex.Store({ ? ? plugins: [createPersistedState({ ? ? ? ? storage: window.sessionStorage, ? ? ? ? // 方法1:用reducer,這里的val是由store里面的所有state,不加reducer為儲(chǔ)存所有,reducer為指定存儲(chǔ) ? ? ? ? reducer(val) ?{ ? ? ? ? ? ? return { ? ? ? ? ? ? ? ? // 只儲(chǔ)存state中的name ? ? ? ? ? ? ? ? keyName: val.name ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? // 方法2:用paths,數(shù)組里面填模塊名,存儲(chǔ)指定模塊 ? ? ? ? // paths: ['Home', 'Order'] ? ? })] })
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue+Openlayer實(shí)現(xiàn)圖形的拖動(dòng)和旋轉(zhuǎn)變形效果
Openlayer具有自己的擴(kuò)展插件ol-ext,可以用來(lái)實(shí)現(xiàn)圖形的拖拽、旋轉(zhuǎn)、縮放、拉伸、移動(dòng)等操作,本文將主要介紹通過(guò)Openlayer實(shí)現(xiàn)圖形的拖動(dòng)和旋轉(zhuǎn),需要的同學(xué)可以學(xué)習(xí)一下2021-11-11vue項(xiàng)目退出登錄清除store數(shù)據(jù)的三種方法
最近使用vue做用戶的登錄/退出,在開發(fā)過(guò)程中遇到的一些問(wèn)題,記錄下來(lái),下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目退出登錄清除store數(shù)據(jù)的三種方法,需要的朋友可以參考下2022-09-09Vue分頁(yè)組件實(shí)現(xiàn)過(guò)程詳解
Web應(yīng)用程序中資源分頁(yè)不僅對(duì)性能很有幫助,而且從用戶體驗(yàn)的角度來(lái)說(shuō)也是非常有用的。在這篇文章中,將了解如何使用Vue創(chuàng)建動(dòng)態(tài)和可用的分頁(yè)組件2022-12-12vue實(shí)現(xiàn)元素拖動(dòng)并互換位置的實(shí)現(xiàn)代碼
在使用Vue的場(chǎng)景下,需要實(shí)現(xiàn)對(duì)元素進(jìn)行拖動(dòng)交換位置,接下來(lái)通過(guò)本文給大家介紹vue實(shí)現(xiàn)元素拖動(dòng)并互換位置的實(shí)現(xiàn)代碼,需要的朋友可以參考下2023-09-09vue-iview動(dòng)態(tài)新增和刪除的方法
這篇文章主要為大家詳細(xì)介紹了vue-iview動(dòng)態(tài)新增和刪除的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06vue cli構(gòu)建的項(xiàng)目中請(qǐng)求代理與項(xiàng)目打包問(wèn)題
這篇文章主要介紹了vue cli構(gòu)建的項(xiàng)目中請(qǐng)求代理與項(xiàng)目打包問(wèn)題,需要的朋友可以參考下2018-02-02