您好,登錄后才能下訂單哦!
這篇“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。