vuex數(shù)據(jù)持久化的兩種實現(xiàn)方案
業(yè)務(wù)需求:
在基于vue開發(fā)SPA項目時,為了解決頁面刷新后數(shù)據(jù)丟失的問題,我們一般都是將數(shù)據(jù)存儲在localstorage或sessionstorage中;當(dāng)數(shù)據(jù)需要全局處理統(tǒng)一管理時,我們也會借助于vue官方提供的vuex來進行數(shù)據(jù)的統(tǒng)一管理。vuex相比localstorage或sessionstorage來說,存儲數(shù)據(jù)更安全些。與此同時,vuex也存在一些弊端,當(dāng)頁面刷新后,vuex中state存儲的數(shù)據(jù)同時也會被更新,vuex中存儲的數(shù)據(jù)不能持久化,需要監(jiān)聽處理來維持vuex存儲的數(shù)據(jù)狀態(tài)持久化。
為解決頁面刷新后vuex中存儲的數(shù)據(jù)狀態(tài)不能持久化的問題,我采取的方案是借助第三方插件工具來實現(xiàn)vuex數(shù)據(jù)的持久化存儲,來解決頁面刷新后數(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({
// 存儲方式:localStorage、sessionStorage、cookies
storage: window.sessionStorage,
// 存儲的 key 的key值
key: "store",
render(state) {
// 要存儲的數(shù)據(jù):本項目采用es6擴展運算符的方式存儲了state中所有的數(shù)據(jù)
return { ...state };
}
})
]
});
export default store;
vuex中module數(shù)據(jù)的持久化存儲
/* 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]
});
注意事項:
- storage為存儲方式,可選值為localStorage、sessionStorage和cookies;
- localStorage和sessionStorage兩種存儲方式可以采用上述代碼中的寫法,若想采用cookies坐位數(shù)據(jù)存儲方式,則需要另外一種寫法;
- render接收一個函數(shù),返回值為一個對象;返回的對象中的鍵值對既是要持久化存儲的數(shù)據(jù);
- 若想持久化存儲部分數(shù)據(jù),請在return的對象中采用key:value鍵值對的方式進行數(shù)據(jù)存儲,render函數(shù)中的參數(shù)既為state對象。
方案二: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)建實例
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é)
上述兩種方案都可以實現(xiàn)vuex數(shù)據(jù)持久化存儲。方案一是我在實際開發(fā)過程中用到的,方案二是在Github上看到的,綜合來說,兩者都可以時間最終的需求,而且都有對應(yīng)的案例Demo可以參考。相比來說方案一在GitHub上的start數(shù)要高于方案二。
請結(jié)合實際情況選擇符合自己的方案!
到此這篇關(guān)于vuex數(shù)據(jù)持久化的兩種實現(xiàn)方案的文章就介紹到這了,更多相關(guān)vuex數(shù)據(jù)持久化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue使用axios發(fā)送請求簡單實現(xiàn)代碼
axios是一個基于Promise的,發(fā)送http請求的一個工具庫,并不是vue中的第三方插件,使用時不能通過“Vue.use()”安裝插件,需要在原型上進行綁定2023-04-04
vue結(jié)合g6實現(xiàn)樹級結(jié)構(gòu)(compactBox?緊湊樹)
本文主要介紹了vue結(jié)合g6實現(xiàn)樹級結(jié)構(gòu),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Vue3+TypeScript+Vite使用require動態(tài)引入圖片等靜態(tài)資源
本文主要介紹了Vue3+TypeScript+Vite使用require動態(tài)引入圖片等靜態(tài)資源,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

