說說Vuex的getters屬性的具體用法
什么是getters
在介紹state中我們了解到,在Store倉庫里,state就是用來存放數(shù)據(jù),若是對數(shù)據(jù)進行處理輸出,比如數(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ù)。馬上想到的方法可能的是:在組件的計算屬性中進行過濾:
<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ā)生變化,還得一一修改這些組件中的計算屬性,很難維護。這種場景下,我們就可以使用 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á)到了,而且只需要在一處維護過濾規(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.js實現(xiàn)插入數(shù)值與表達(dá)式的方法分析
這篇文章主要介紹了vue.js實現(xiàn)插入數(shù)值與表達(dá)式的方法,結(jié)合實例形式分析了vue.js常見的3種插入數(shù)值實現(xiàn)方式,并總結(jié)了vue.js插值與表達(dá)式相關(guān)使用技巧,需要的朋友可以參考下2018-07-07vue項目中echarts自適應(yīng)問題的高級解決過程
雖然老早就看過很多echarts的例子,但自己接觸的項目中一直都沒有真正用到過,直到最近才開始真正使用,下面這篇文章主要給大家介紹了關(guān)于vue項目中echarts自適應(yīng)問題的高級解決過程,需要的朋友可以參考下2023-05-05VUEJS 2.0 子組件訪問/調(diào)用父組件的實例
下面小編就為大家分享一篇VUEJS 2.0 子組件訪問/調(diào)用父組件的實例。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02Vue監(jiān)聽localstorage變化的方法詳解
在日常開發(fā)中,我們經(jīng)常使用localStorage來存儲一些變量,這些變量會存儲在瀏覽中,對于localStorage來說,即使關(guān)閉瀏覽器,這些變量依然存儲著,方便我們開發(fā)的時候在別的地方使用,本文就給大家介紹Vue如何監(jiān)聽localstorage的變化,需要的朋友可以參考下2023-10-10vant組件表單外部的button觸發(fā)form表單的submit事件問題
這篇文章主要介紹了vant組件表單外部的button觸發(fā)form表單的submit事件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06