您好,登錄后才能下訂單哦!
這篇文章主要介紹“vuex輔助函數mapGetters如何使用”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“vuex輔助函數mapGetters如何使用”文章能幫助大家解決問題。
mapGetters
輔助函數
mapGetters
輔助函數僅僅是將 store 中的 getter 映射到局部計算屬性:
<!-- test.vue --> <template> <div class="vuexReponse"> <div @click="changeVal">點擊</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 界面中點擊調用changeVal(), changeVal方法通過commite 傳參val 并調用 store.js中的mutationsHello() 方法,mutationsHello方法修改state中的stateHello的值,在getters 的 gettersHello 中監聽stateHello的值,stateHello的值的改變觸發了gettersHello,在test.vue界面computed 中計算了 store.getters.gettersHello ,這個就將gettersHello 映射到 store.gettes.gettersHello 的值,通過模板 將gettersHello 渲染到dom中,同時由于gettersHello 的改變也能觸發watch中gettersHello,實現對store.getters.gettersHello 數據改變的監聽。
<!-- moduleTest.vue --> <template> <div class="vuexReponse"> <div @click="changeVal">點擊</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 中計算映射 模塊中的getters 的方法時 調用方式與 獲取模塊中的state 中的數據不同
this.$store.getters["vuexTest/gettersHello"]
/** * 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">點擊</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"]), // 數組形式 ...mapGetters({ // 對象形式 gettersHello: "gettersHello" }), ...mapGetters({ gettersHelloOther: "gettersHello" // 對象形式下 改變映射 }), }, methods: { changeVal() { this.$store.commit("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: { } }
<!-- module test.vue --> <template> <div class="vuexReponse"> <div @click="changeVal">點擊</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"]), // 數組形式 ...mapGetters("vuexTest", { // 對象形式 gettersHello: "gettersHello" }), ...mapGetters("vuexTest", { gettersHelloOther: "gettersHello" // 對象形式下 改變映射 }), }, methods: { changeVal() { this.$store.commit("vuexTest/mutationsHello", 2) } } } </script>
這三種形式 ...mapGetters(["vuexTest/gettersHello"]), // 數組形式 ...mapGetters("vuexTest", { // 對象形式 gettersHello: "gettersHello" }), ...mapGetters("vuexTest", { gettersHelloOther: "gettersHello" // 對象形式下 改變映射 }),
關于“vuex輔助函數mapGetters如何使用”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。