最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vuex?store?緩存存儲(chǔ)原理分析

 更新時(shí)間:2022年07月27日 16:57:38   作者:Osborn521  
這篇文章主要介紹了vuex?store?緩存存儲(chǔ)原理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

vuex store 緩存存儲(chǔ)原理

vuex 的設(shè)計(jì)是將數(shù)據(jù)存在一個(gè)對(duì)象樹(shù)的變量中,我們的應(yīng)用(vue應(yīng)用)從這個(gè)變量中取數(shù)據(jù),然后供應(yīng)用使用,當(dāng)將當(dāng)前頁(yè)面關(guān)閉, vuex 中的變量會(huì)隨著消失,重新打開(kāi)頁(yè)面的時(shí)候,需要重新生成。

而,瀏覽器緩存(cookie,localstorage等)是將數(shù)據(jù)存到瀏覽器的某個(gè)地方,關(guān)閉頁(yè)面,不會(huì)自動(dòng)清空這些數(shù)據(jù),當(dāng)再次打開(kāi)這個(gè)頁(yè)面時(shí),還是能取到之前存在瀏覽器上的數(shù)據(jù)(cookie,localstorage等)。

要使用 vuex 還是使用瀏覽器緩存,要看具體的業(yè)務(wù)場(chǎng)景。比如:像用戶(hù)校驗(yàn)的 token 就可以存在 cookie 中,因?yàn)橛脩?hù)再次登錄的時(shí)候能用到。而像用戶(hù)的權(quán)限數(shù)據(jù),這些是有一定安全性考慮,且不同用戶(hù)的權(quán)限不同,放在 vuex 中更合理,用戶(hù)退出時(shí),自動(dòng)銷(xiāo)毀。

其次,vuex 中的 state 是單向的,也可以異步操作,這兩個(gè)沒(méi)有沖突。

vuex 中的 state 的設(shè)計(jì)思路是保證數(shù)據(jù)的一致性和連續(xù)性,而讓 state 中的值只能通過(guò) action 來(lái)發(fā)起 commit,進(jìn)而改變 state 中的值。

而,action 中是 同步 還是 異步,都是單向地改變 state 中的值。 

如何使用store緩存數(shù)據(jù)

this.$store.commit('方法名',值)【存儲(chǔ)】
this.$store.state.方法名【取值】

我使用的是 vue-element-admin

1 src/store/index.js 寫(xiě)方法

import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'
Vue.use(Vuex)
const modulesFiles = require.context('./modules', true, /\.js$/)
const modules = modulesFiles.keys().reduce((modules, modulePath) => {
? const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
? const value = modulesFiles(modulePath)
? modules[moduleName] = value.default
? return modules
}, {})
const store = new Vuex.Store({
? modules,
? getters,
? state: { ? ? ? ? ? ? ? // SET_IMPORT 設(shè)置的這
? ? imports: []
? },
? mutations: {
? ? SET_IMPORT(state, routes) { ? ? ?// 設(shè)置 SET_IMPORT方法,方法中設(shè)置存儲(chǔ)某個(gè)值
? ? ? state.imports = routes
? ? }
? }
})
export default store

2 src/store/getter.js

const getters = {
? sidebar: state => state.app.sidebar,
? errorLogs: state => state.errorLog.logs,
? imports: state => state.imports ? ?// 存儲(chǔ)上面設(shè)置的 imports 字段
}
export default getters

3 使用 store 存取數(shù)據(jù)

const nullim = []
? ? var myMap = {}
? ? // 塞入鍵值對(duì),當(dāng)前上傳文件
? ? myMap['filename'] = rawFile.name
? ? myMap['result'] = 0
? ? nullim.push(myMap)
this.$store.commit('SET_IMPORT', nullim) ? ?// 存入緩存,調(diào)用 SET_IMPORT 方法
const old_imports = this.$store.getters.imports ? ? ? ?// 取緩存中數(shù)據(jù),通過(guò)字段名

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。 

相關(guān)文章

最新評(píng)論

达州市| 太白县| 丹凤县| 商洛市| 阳江市| 本溪| 寿阳县| 丰顺县| 阿拉善盟| 航空| 呼玛县| 新宾| 凌海市| 平远县| 北海市| 肃北| 茌平县| 颍上县| 邻水| 平度市| 花垣县| 尚志市| 辛集市| 图片| 旅游| 太康县| 白山市| 丹江口市| 宜章县| 万载县| 辰溪县| 平邑县| 白银市| 无锡市| 诸城市| 通化县| 北辰区| 武陟县| 阜新市| 姚安县| 建德市|