解決vuex數(shù)據(jù)丟失問(wèn)題
數(shù)據(jù)丟失的原因
vuex存儲(chǔ)的數(shù)據(jù)只是在頁(yè)面中,相當(dāng)于全局變量,頁(yè)面刷新的時(shí)候vuex里的數(shù)據(jù)會(huì)重新初始化,導(dǎo)致數(shù)據(jù)丟失。
因?yàn)関uex里的數(shù)據(jù)是保存在運(yùn)行內(nèi)存中的,當(dāng)頁(yè)面刷新時(shí),頁(yè)面會(huì)重新加載vue實(shí)例,vuex里面的數(shù)據(jù)就會(huì)被重新賦值。
方法1:使用第三方庫(kù) vuex-persistedstate
npm install --save vuex-persistedstate
01 store / index.js 之 localStorage
- 注意點(diǎn): vuex-persistedstate默認(rèn)存儲(chǔ)在 localStorage之中,基本上不需要配置什么
import createPersistedState from "vuex-persistedstate"
const store =newVuex.Store({
state: {
cartList: [],
},
mutations: {},
actions: {},
// 當(dāng)state中的值發(fā)生改變,此時(shí)localStorage中的vuex的值會(huì)同步把state中的所有值存儲(chǔ)起來(lái),當(dāng)頁(yè)面刷
新的時(shí)候,state的值會(huì)從localStorage自動(dòng)獲取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的所有值存儲(chǔ)到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) ?{
? ? ? ? ?// 此時(shí),當(dāng)count發(fā)生改變的時(shí)候,就會(huì)調(diào)用此函數(shù),并且val的值為當(dāng)前state對(duì)象,return的值為當(dāng)前本地存儲(chǔ)的value值(本地存儲(chǔ)的key值為vuex)
? ? ? ? ?return {
? ? ? ? ? ? ?count: val.count,
? ?changeCount: 'aaa'
? ? ? ? ?}
? ? ?}
?})]
})方法2 把state的數(shù)據(jù)先緩存到localStorage之中,頁(yè)面刷新的時(shí)候,拿到數(shù)據(jù)寫(xiě)入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))
},
}
})頁(yè)面刷新的時(shí)候
通過(guò)監(jiān)聽(tīng)beforeunload事件來(lái)進(jìn)行數(shù)據(jù)的localStorage存儲(chǔ),beforeunload事件在頁(yè)面刷新時(shí)進(jìn)行觸發(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ù)丟失問(wèn)題的文章就介紹到這了,更多相關(guān)vuex 數(shù)據(jù)丟失內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vuex刷新之后數(shù)據(jù)丟失,數(shù)據(jù)持久化,vuex-persistedstate問(wèn)題
- vuex頁(yè)面刷新數(shù)據(jù)丟失問(wèn)題的四種解決方式
- 解決vue頁(yè)面刷新vuex中state數(shù)據(jù)丟失的問(wèn)題
- vuex結(jié)合session存儲(chǔ)數(shù)據(jù)解決頁(yè)面刷新數(shù)據(jù)丟失問(wèn)題
- 關(guān)于vuex強(qiáng)刷數(shù)據(jù)丟失問(wèn)題解析
- vuex頁(yè)面刷新導(dǎo)致數(shù)據(jù)丟失的解決方案
- vuex刷新后數(shù)據(jù)丟失的解決方法
- vuex存儲(chǔ)復(fù)雜參數(shù)(如對(duì)象數(shù)組等)刷新數(shù)據(jù)丟失的解決方法
- vuex頁(yè)面刷新后數(shù)據(jù)丟失的方法
相關(guān)文章
基于canvas實(shí)現(xiàn)手寫(xiě)簽名(vue)
這篇文章主要為大家詳細(xì)介紹了基于canvas實(shí)現(xiàn)簡(jiǎn)易的手寫(xiě)簽名,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
使用Vue如何寫(xiě)一個(gè)雙向數(shù)據(jù)綁定(面試常見(jiàn))
這篇文章主要介紹了使用Vue如何寫(xiě)一個(gè)雙向數(shù)據(jù)綁定,在前端面試過(guò)程中經(jīng)常會(huì)問(wèn)到,文中主要實(shí)現(xiàn)v-model,v-bind 和v-click三個(gè)命令,其他命令也可以自行補(bǔ)充。需要的朋友可以參考下2018-04-04
elementUI如何動(dòng)態(tài)給el-tree添加子節(jié)點(diǎn)數(shù)據(jù)children詳解
element-ui 目前基本成為前端pc網(wǎng)頁(yè)端標(biāo)準(zhǔn)ui框架,下面這篇文章主要給大家介紹了關(guān)于elementUI如何動(dòng)態(tài)給el-tree添加子節(jié)點(diǎn)數(shù)據(jù)children的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
詳解Jest結(jié)合Vue-test-utils使用的初步實(shí)踐
這篇文章主要介紹了詳解Jest結(jié)合Vue-test-utils使用的初步實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
前端文件導(dǎo)出設(shè)置responseType為blob時(shí)遇到的問(wèn)題及解決
這篇文章主要給大家介紹了關(guān)于前端文件導(dǎo)出設(shè)置responseType為blob時(shí)遇到的問(wèn)題及解決方法,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09
vue如何通過(guò)image-conversion實(shí)現(xiàn)圖片壓縮詳解
在Vue項(xiàng)目中上傳大圖片時(shí),可以通過(guò)image-conversion庫(kù)壓縮至指定大小,這篇文章主要介紹了vue如何通過(guò)image-conversion實(shí)現(xiàn)圖片壓縮的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-12-12
vue-resource調(diào)用promise取數(shù)據(jù)方式詳解
這篇文章主要介紹了vue-resource調(diào)用promise取數(shù)據(jù)方式詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07

