說說Vuex的getters屬性的具體用法
什么是getters
在介紹state中我們了解到,在Store倉庫里,state就是用來存放數(shù)據(jù),若是對數(shù)據(jù)進(jìn)行處理輸出,比如數(shù)據(jù)要過濾,一般我們可以寫到computed中。但是如果很多組件都使用這個過濾后的數(shù)據(jù),比如餅狀圖組件和曲線圖組件,我們是否可以把這個數(shù)據(jù)抽提出來共享?這就是getters存在的意義。我們可以認(rèn)為,【getters】是store的計算屬性。
源碼分析
wrapGetters初始化getters,接受3個參數(shù),store表示當(dāng)前的Store實例,moduleGetters當(dāng)前模塊下所有的getters,modulePath對應(yīng)模塊的路徑
function `wrapGetters` (store, moduleGetters, modulePath) {
Object.keys(moduleGetters).forEach(getterKey => {
// 遍歷先所有的getters
const rawGetter = moduleGetters[getterKey]
if (store._wrappedGetters[getterKey]) {
console.error(`[vuex] duplicate getter key: ${getterKey}`)
// getter的key不允許重復(fù),否則會報錯
return
}
store._wrappedGetters[getterKey] = function `wrappedGetter` (store{
// 將每一個getter包裝成一個方法,并且添加到store._wrappedGetters對象中,
return rawGetter(
//執(zhí)行g(shù)etter的回調(diào)函數(shù),傳入三個參數(shù),(local state,store getters,rootState)
getNestedState(store.state, modulePath), // local state
//根據(jù)path查找state上嵌套的state
store.getters,
// store上所有的getters
store.state
// root state)}})
}
//根據(jù)path查找state上嵌套的state
function `getNestedState` (state, path) {
return path.length
? path.reduce((state, key) => state[key], state): state}
1 應(yīng)用場景
假設(shè)我們在 Vuex 中定義了一個數(shù)組:
const store = new Vuex.Store({
state: {
list:[1,3,5,7,9,20,30]
}
...
})
業(yè)務(wù)場景希望過濾出大于 5 的數(shù)。馬上想到的方法可能的是:在組件的計算屬性中進(jìn)行過濾:
<template>
<div>
{{list}}
</div>
</template>
<script>
export default {
name: "index.vue",
computed: {
list() {
return this.$store.state.list.filter(item => item > 5);
}
}
}
</script>
效果:

功能雖然實現(xiàn)了,但如果其它組件也需要過濾后的數(shù)據(jù),那么就得把 index.vue 中的計算過濾代碼復(fù)制出來。如果過濾規(guī)則發(fā)生變化,還得一一修改這些組件中的計算屬性,很難維護(hù)。這種場景下,我們就可以使用 getters 屬性啦O(∩_∩)O~
2 基礎(chǔ)用法
main.js:
const store = new Vuex.Store({
state: {
list: [1, 3, 5, 7, 9, 20, 30]
},
getters: {
filteredList: state => {
return state.list.filter(item => item > 5)
}
}
})
index.vue:
<script>
export default {
name: "index.vue",
computed: {
list() {
return this.$store.getters.filteredList;
}
}
}
</script>
效果達(dá)到了,而且只需要在一處維護(hù)過濾規(guī)則即可。
3 內(nèi)部依賴
getter 可以依賴其它已經(jīng)定義好的 getter。比如我們需要統(tǒng)計過濾后的列表數(shù)量,就可以依賴之前定義好的過濾函數(shù)。
main.js:
const store = new Vuex.Store({
state: {
list: [1, 3, 5, 7, 9, 20, 30]
},
getters: {
filteredList: state => {
return state.list.filter(item => item > 5)
},
listCount: (state, getters) => {
return getters.filteredList.length;
}
}
})
index.vue:
<template>
<div>
過濾后的列表:{{list}}
<br>
列表長度:{{listCount}}
</div>
</template>
<script>
export default {
name: "index.vue",
computed: {
list() {
return this.$store.getters.filteredList;
},
listCount() {
return this.$store.getters.listCount;
}
}
}
</script>
效果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue動態(tài)生成el-checkbox點擊無法賦值的解決方法
這篇文章主要給大家介紹了關(guān)于Vue動態(tài)生成el-checkbox點擊無法賦值的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
Ant?Design?Vue?修改表格頭部樣式的詳細(xì)代碼
這篇文章主要介紹了Ant?Design?Vue?修改表格頭部樣式,首先用到的是customHeaderRow這個API,類型是一個函數(shù),本文通過完整代碼給大家詳細(xì)講解,需要的朋友可以參考下2022-10-10
基于vue實現(xiàn)多引擎搜索及關(guān)鍵字提示
這篇文章主要為大家詳細(xì)介紹了基于vue實現(xiàn)多引擎搜索及關(guān)鍵字提示的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03

