vuex數(shù)據(jù)持久化的兩種實(shí)現(xiàn)方案
業(yè)務(wù)需求:
在基于vue開發(fā)SPA項(xiàng)目時(shí),為了解決頁面刷新后數(shù)據(jù)丟失的問題,我們一般都是將數(shù)據(jù)存儲(chǔ)在localstorage或sessionstorage中;當(dāng)數(shù)據(jù)需要全局處理統(tǒng)一管理時(shí),我們也會(huì)借助于vue官方提供的vuex來進(jìn)行數(shù)據(jù)的統(tǒng)一管理。vuex相比localstorage或sessionstorage來說,存儲(chǔ)數(shù)據(jù)更安全些。與此同時(shí),vuex也存在一些弊端,當(dāng)頁面刷新后,vuex中state存儲(chǔ)的數(shù)據(jù)同時(shí)也會(huì)被更新,vuex中存儲(chǔ)的數(shù)據(jù)不能持久化,需要監(jiān)聽處理來維持vuex存儲(chǔ)的數(shù)據(jù)狀態(tài)持久化。
為解決頁面刷新后vuex中存儲(chǔ)的數(shù)據(jù)狀態(tài)不能持久化的問題,我采取的方案是借助第三方插件工具來實(shí)現(xiàn)vuex數(shù)據(jù)的持久化存儲(chǔ),來解決頁面刷新后數(shù)據(jù)更新的問題。
方案一:vuex-persistedstate
安裝插件
yarn add vuex-persistedstate // 或 npm install --save vuex-persistedstate
使用方法
import Vuex from "vuex";
// 引入插件
import createPersistedState from "vuex-persistedstate";
Vue.use(Vuex);
const state = {};
const mutations = {};
const actions = {};
const store = new Vuex.Store({
state,
mutations,
actions,
/* vuex數(shù)據(jù)持久化配置 */
plugins: [
createPersistedState({
// 存儲(chǔ)方式:localStorage、sessionStorage、cookies
storage: window.sessionStorage,
// 存儲(chǔ)的 key 的key值
key: "store",
render(state) {
// 要存儲(chǔ)的數(shù)據(jù):本項(xiàng)目采用es6擴(kuò)展運(yùn)算符的方式存儲(chǔ)了state中所有的數(shù)據(jù)
return { ...state };
}
})
]
});
export default store;
vuex中module數(shù)據(jù)的持久化存儲(chǔ)
/* module.js */
export const dataStore = {
state: {
data: []
}
}
/* store.js */
import { dataStore } from './module'
const dataState = createPersistedState({
paths: ['data']
});
export new Vuex.Store({
modules: {
dataStore
},
plugins: [dataState]
});
注意事項(xiàng):
- storage為存儲(chǔ)方式,可選值為localStorage、sessionStorage和cookies;
- localStorage和sessionStorage兩種存儲(chǔ)方式可以采用上述代碼中的寫法,若想采用cookies坐位數(shù)據(jù)存儲(chǔ)方式,則需要另外一種寫法;
- render接收一個(gè)函數(shù),返回值為一個(gè)對(duì)象;返回的對(duì)象中的鍵值對(duì)既是要持久化存儲(chǔ)的數(shù)據(jù);
- 若想持久化存儲(chǔ)部分?jǐn)?shù)據(jù),請(qǐng)?jiān)趓eturn的對(duì)象中采用key:value鍵值對(duì)的方式進(jìn)行數(shù)據(jù)存儲(chǔ),render函數(shù)中的參數(shù)既為state對(duì)象。
方案二:vuex-persist
安裝插件
yarn add vuex-persist // 或 npm install --save vuex-persist
使用方法
import Vuex from "vuex";
// 引入插件
import VuexPersistence from "vuex-persist";
Vue.use(Vuex);
// 初始化
const state = {
userName:'admin'
};
const mutations = {};
const actions = {};
// 創(chuàng)建實(shí)例
const vuexPersisted = new VuexPersistence({
storage: window.sessionStorage,
render:state=>({
userName:state.userName
// 或
...state
})
});
const store = new Vuex.Store({
state,
actions,
mutations,
// 數(shù)據(jù)持久化設(shè)置
plugins:[vuexPersisted.plugins]
});
export default store;
屬性方法
| 屬性值 | 數(shù)據(jù)類型 | 描述 |
|---|---|---|
| key | string | The key to store the state in the storage_Default: 'vuex'_ |
| storage | Storage (Web API) | localStorage, sessionStorage, localforage or your custom Storage object.Must implement getItem, setItem, clear etc.Default: window.localStorage |
| saveState | function(key, state[, storage]) | If not using storage, this custom function handles saving state to persistence |
| reducer | function(state) => object | State reducer. reduces state to only those values you want to save. By default, saves entire state |
| filter | function(mutation) => boolean | Mutation filter. Look at mutation.type and return true for only those ones which you want a persistence write to be triggered for. Default returns true for all mutations |
| modules | string[] | List of modules you want to persist. (Do not write your own reducer if you want to use this) |
| asyncStorage | boolean | Denotes if the store uses Promises (like localforage) or not (you must set this to true when suing something like localforage)Default: false |
| supportCircular | boolean | Denotes if the state has any circular references to it self(state.x === state)Default: false |
總結(jié)
上述兩種方案都可以實(shí)現(xiàn)vuex數(shù)據(jù)持久化存儲(chǔ)。方案一是我在實(shí)際開發(fā)過程中用到的,方案二是在Github上看到的,綜合來說,兩者都可以時(shí)間最終的需求,而且都有對(duì)應(yīng)的案例Demo可以參考。相比來說方案一在GitHub上的start數(shù)要高于方案二。
請(qǐng)結(jié)合實(shí)際情況選擇符合自己的方案!
到此這篇關(guān)于vuex數(shù)據(jù)持久化的兩種實(shí)現(xiàn)方案的文章就介紹到這了,更多相關(guān)vuex數(shù)據(jù)持久化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue使用axios發(fā)送請(qǐng)求簡單實(shí)現(xiàn)代碼
axios是一個(gè)基于Promise的,發(fā)送http請(qǐng)求的一個(gè)工具庫,并不是vue中的第三方插件,使用時(shí)不能通過“Vue.use()”安裝插件,需要在原型上進(jìn)行綁定2023-04-04
Vue程序化的事件監(jiān)聽器(實(shí)例方案詳解)
本文通過兩種方案給大家介紹了Vue程序化的事件監(jiān)聽器,每種方案通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-01-01
vue項(xiàng)目移動(dòng)端實(shí)現(xiàn)ip輸入框問題
這篇文章主要介紹了vue項(xiàng)目移動(dòng)端實(shí)現(xiàn)ip輸入框問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03
vue結(jié)合g6實(shí)現(xiàn)樹級(jí)結(jié)構(gòu)(compactBox?緊湊樹)
本文主要介紹了vue結(jié)合g6實(shí)現(xiàn)樹級(jí)結(jié)構(gòu),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
詳解Vue如何實(shí)現(xiàn)響應(yīng)式布局
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)響應(yīng)式布局的兩種方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下2023-12-12
Vue3+TypeScript+Vite使用require動(dòng)態(tài)引入圖片等靜態(tài)資源
本文主要介紹了Vue3+TypeScript+Vite使用require動(dòng)態(tài)引入圖片等靜態(tài)資源,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

