vuex 中輔助函數(shù)mapGetters的基本用法詳解
mapGetters
輔助函數(shù)
mapGetters
輔助函數(shù)僅僅是將 store 中的 getter 映射到局部計(jì)算屬性:
1、在組件或界面中不使用mapGetter調(diào)用映射vuex中的getter
1.1 調(diào)用映射根部store中的getter
<!-- test.vue --> <template> <div class="vuexReponse"> <div @click="changeVal">點(diǎn)擊</div> <div>"stateHello: "{{stateHello}}</div> <div>"gettersHello: "{{gettersHello}}</div> </div> </template> <script> export default { watch: { gettersHello(newVal, oldVal) { console.log("gettersHello newVal", newVal); console.log("gettersHello oldVal", oldVal); } }, computed: { stateHello() { return this.$store.state.stateHello }, gettersHello() { return this.$store.getters.gettersHello } }, methods: { changeVal() { this.$store.commit("mutationsHello", 2) } } } </script>
/** * store.js */ import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ state: { stateHello: 1 }, getters: { gettersHello: (state) => { return state.stateHello * 2 } }, mutations: { mutationsHello(state, val) { console.log("val", val); // 2 state.stateHello += val } }, })
流程: 在test.vue 界面中點(diǎn)擊調(diào)用changeVal(), changeVal方法通過(guò)commite 傳參val 并調(diào)用 store.js中的mutationsHello() 方法,mutationsHello方法修改state中的stateHello的值,在getters 的 gettersHello 中監(jiān)聽stateHello的值,stateHello的值的改變觸發(fā)了gettersHello,在test.vue界面computed 中計(jì)算了 store.getters.gettersHello ,這個(gè)就將gettersHello 映射到 store.gettes.gettersHello 的值,通過(guò)模板 將gettersHello 渲染到dom中,同時(shí)由于gettersHello 的改變也能觸發(fā)watch中g(shù)ettersHello,實(shí)現(xiàn)對(duì)store.getters.gettersHello 數(shù)據(jù)改變的監(jiān)聽。
1.2 調(diào)用映射modules模塊store中的getter
<!-- moduleTest.vue --> <template> <div class="vuexReponse"> <div @click="changeVal">點(diǎn)擊</div> <div>stateHello: {{stateHello}}</div> <div>gettersHello: {{gettersHello}}</div> </div> </template> <script> export default { watch: { gettersHello(newVal, oldVal) { console.log("gettersHello newVal", newVal); console.log("gettersHello oldVal", oldVal); } }, computed: { stateHello() { return this.$store.state.vuexTest.stateHello }, gettersHello() { return this.$store.getters['vuexTest/gettersHello'] } }, methods: { changeVal() { this.$store.commit("vuexTest/mutationsHello", 2) } } } </script>
/** * 模塊 vuexTest.js */ export default { namespaced: true, state: { stateHello: 1, }, getters: { gettersHello: (state, getters, rootState, rootGetters) => { console.log("state", state); console.log("getters", getters); console.log("rootState", rootState); console.log("rootGetters", rootGetters); return state.stateHello * 2 } }, mutations: { mutationsHello(state, val) { console.log("1111"); console.log("val", val); state.stateHello += val } }, actions: { } }
需要注意的地方是在 computed 中計(jì)算映射 模塊中的getters 的方法時(shí) 調(diào)用方式與 獲取模塊中的state 中的數(shù)據(jù)不同
this.$store.getters['vuexTest/gettersHello']
2、在組件或界面中使用mapGetter調(diào)用映射vuex中的getter
2.1 調(diào)用映射根部store中的getter
/** * store.js */ import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ state: { stateHello: 1 }, getters: { gettersHello: (state) => { return state.stateHello * 2 } }, mutations: { mutationsHello(state, val) { state.stateHello += val } }, })
<!-- Test.vue --> <template> <div class="vuexReponse"> <div @click="changeVal">點(diǎn)擊</div> <div>stateHello: {{stateHello}}</div> <div>gettersHello: {{gettersHello}}</div> <div>gettersHelloOther {{gettersHelloOther}}</div> </div> </template> <script> import { mapGetters } from "vuex"; export default { name: "vuexReponse", components: { }, data() { return { } }, watch: { gettersHello(newVal, oldVal) { console.log("gettersHello newVal", newVal); console.log("gettersHello oldVal", oldVal); } }, computed: { stateHello() { return this.$store.state.stateHello }, ...mapGetters(["gettersHello"]), // 數(shù)組形式 ...mapGetters({ // 對(duì)象形式 gettersHello: "gettersHello" }), ...mapGetters({ gettersHelloOther: "gettersHello" // 對(duì)象形式下 改變映射 }), }, methods: { changeVal() { this.$store.commit("mutationsHello", 2) } } } </script>
2.2 調(diào)用映射根部store中的getter
/** * vuexTest.js */ export default { namespaced: true, state: { stateHello: 1, }, getters: { gettersHello: (state, getters, rootState, rootGetters) => { console.log("state", state); console.log("getters", getters); console.log("rootState", rootState); console.log("rootGetters", rootGetters); return state.stateHello * 2 } }, mutations: { mutationsHello(state, val) { console.log("1111"); console.log("val", val); state.stateHello += val } }, actions: { } }
<!-- module test.vue --> <template> <div class="vuexReponse"> <div @click="changeVal">點(diǎn)擊</div> <div>stateHello: {{stateHello}}</div> <div>gettersHello: {{gettersHello}}</div> <div>gettersHelloOther {{gettersHelloOther}}</div> </div> </template> <script> import { mapGetters } from "vuex"; export default { name: "vuexReponse", watch: { gettersHello(newVal, oldVal) { console.log("gettersHello newVal", newVal); console.log("gettersHello oldVal", oldVal); } }, computed: { stateHello() { return this.$store.state.vuexTest.stateHello }, ...mapGetters(["vuexTest/gettersHello"]), // 數(shù)組形式 ...mapGetters("vuexTest", { // 對(duì)象形式 gettersHello: "gettersHello" }), ...mapGetters("vuexTest", { gettersHelloOther: "gettersHello" // 對(duì)象形式下 改變映射 }), }, methods: { changeVal() { this.$store.commit("vuexTest/mutationsHello", 2) } } } </script>
這三種形式 ...mapGetters(["vuexTest/gettersHello"]), // 數(shù)組形式 ...mapGetters("vuexTest", { // 對(duì)象形式 gettersHello: "gettersHello" }), ...mapGetters("vuexTest", { gettersHelloOther: "gettersHello" // 對(duì)象形式下 改變映射 }),
到此這篇關(guān)于vuex 中輔助函數(shù)mapGetters的基本用法詳解的文章就介紹到這了,更多相關(guān)vuex mapGetters使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue3使用Vuex之mapState與mapGetters詳解
- 記一次vuex的mapGetters無(wú)效原因及解決
- vuex中...mapstate和...mapgetters的區(qū)別及說(shuō)明
- vue3.0使用mapState,mapGetters和mapActions的方式
- vuex2中使用mapGetters/mapActions報(bào)錯(cuò)的解決方法
- vuex中的 mapState,mapGetters,mapActions,mapMutations 的使用
- 詳解vuex中mapState,mapGetters,mapMutations,mapActions的作用
- Vue Getters和mapGetters的原理及使用示例詳解
相關(guān)文章
詳解Vue-Cli 異步加載數(shù)據(jù)的一些注意點(diǎn)
本篇文章主要介紹了詳解Vue-Cli 異步加載數(shù)據(jù)的一些注意點(diǎn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08Vue中的路由導(dǎo)航守衛(wèi)導(dǎo)航解析流程
這篇文章主要介紹了Vue中的路由導(dǎo)航守衛(wèi)導(dǎo)航解析流程,正如其名,vue-router 提供的導(dǎo)航守衛(wèi)主要用來(lái)通過(guò)跳轉(zhuǎn)或取消的方式守衛(wèi)導(dǎo)航。這里有很多方式植入路由導(dǎo)航中:全局的,單個(gè)路由獨(dú)享的,或者組件級(jí)的2023-04-04vue-cli axios請(qǐng)求方式及跨域處理問(wèn)題
這篇文章主要介紹了vue-cli axios請(qǐng)求方式及跨域處理問(wèn)題,文中還給大家提到了vue中axios解決跨域問(wèn)題和攔截器使用,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-03-03Vue入門之?dāng)?shù)據(jù)綁定(小結(jié))
本篇文章主要介紹了探索Vue高階組件的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Vue如何整合mavon-editor編輯器(markdown編輯和預(yù)覽)
這篇文章主要介紹了Vue整合mavon-editor編輯器(markdown編輯和預(yù)覽)的相關(guān)知識(shí),mavon-editor是目前比較主流的markdown編輯器,重點(diǎn)介紹它的使用方法,需要的朋友可以參考下2022-10-10