欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vuex數(shù)據(jù)持久化的兩種實現(xiàn)方案

 更新時間:2021年07月13日 11:49:36   作者:下落香樟樹  
在vuex的時候刷新以后里面存儲的state就會被瀏覽器釋放掉,因為我們的state都是存儲在內(nèi)存中的,所以一刷新頁面就會把state中的數(shù)據(jù)重置,這就涉及到vue數(shù)據(jù)持久化的問題,這篇文章主要給大家介紹了關(guān)于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]
});

注意事項:

  1. storage為存儲方式,可選值為localStorage、sessionStorage和cookies;
  2. localStorage和sessionStorage兩種存儲方式可以采用上述代碼中的寫法,若想采用cookies坐位數(shù)據(jù)存儲方式,則需要另外一種寫法;
  3. render接收一個函數(shù),返回值為一個對象;返回的對象中的鍵值對既是要持久化存儲的數(shù)據(jù);
  4. 若想持久化存儲部分數(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)文章

  • petite?vue的使用示例詳解

    petite?vue的使用示例詳解

    這篇文章主要為大家介紹了petite?vue的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Vue使用axios發(fā)送請求簡單實現(xiàn)代碼

    Vue使用axios發(fā)送請求簡單實現(xiàn)代碼

    axios是一個基于Promise的,發(fā)送http請求的一個工具庫,并不是vue中的第三方插件,使用時不能通過“Vue.use()”安裝插件,需要在原型上進行綁定
    2023-04-04
  • vue3如何用pinia替代vuex

    vue3如何用pinia替代vuex

    這篇文章主要介紹了vue3如何使用pinia替代vuex問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • vue-cli常用設(shè)置總結(jié)

    vue-cli常用設(shè)置總結(jié)

    本文給大家總結(jié)了vue-cli常用設(shè)置,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2018-02-02
  • Vue程序化的事件監(jiān)聽器(實例方案詳解)

    Vue程序化的事件監(jiān)聽器(實例方案詳解)

    本文通過兩種方案給大家介紹了Vue程序化的事件監(jiān)聽器,每種方案通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2020-01-01
  • vue項目移動端實現(xiàn)ip輸入框問題

    vue項目移動端實現(xiàn)ip輸入框問題

    這篇文章主要介紹了vue項目移動端實現(xiàn)ip輸入框問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • Vue的虛擬DOM和diff算法你了解嗎

    Vue的虛擬DOM和diff算法你了解嗎

    這篇文章主要為大家詳細介紹了Vue的虛擬DOM和diff算法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • vue結(jié)合g6實現(xiàn)樹級結(jié)構(gòu)(compactBox?緊湊樹)

    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
  • 詳解Vue如何實現(xiàn)響應(yīng)式布局

    詳解Vue如何實現(xiàn)響應(yīng)式布局

    這篇文章主要為大家詳細介紹了Vue實現(xiàn)響應(yīng)式布局的兩種方法,文中的示例代碼講解詳細,具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-12-12
  • Vue3+TypeScript+Vite使用require動態(tài)引入圖片等靜態(tài)資源

    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

最新評論