最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vuex 中輔助函數(shù)mapGetters的基本用法詳解

 更新時(shí)間:2021年07月07日 15:01:48   作者:只爭(zhēng)朝夕,不負(fù)韶華  
mapGetters輔助函數(shù)僅僅是將 store 中的 getter 映射到局部計(jì)算屬性,在組件或界面中不使用mapGetter調(diào)用映射vuex中的getter,在組件或界面中使用mapGetter調(diào)用映射vuex中的getter,具體內(nèi)容跟隨小編一起通過(guò)本文學(xué)習(xí)吧 

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)聽(tīng)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)聽(tīng)。

  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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue中動(dòng)態(tài)路由加載組件,找不到module問(wèn)題及解決

    vue中動(dòng)態(tài)路由加載組件,找不到module問(wèn)題及解決

    這篇文章主要介紹了vue中動(dòng)態(tài)路由加載組件,找不到module問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • vue組件引用另一個(gè)組件出現(xiàn)組件不顯示的問(wèn)題及解決

    vue組件引用另一個(gè)組件出現(xiàn)組件不顯示的問(wèn)題及解決

    這篇文章主要介紹了vue組件引用另一個(gè)組件出現(xiàn)組件不顯示的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue+golang實(shí)現(xiàn)上傳微信頭像功能

    vue+golang實(shí)現(xiàn)上傳微信頭像功能

    這篇文章主要介紹了vue+golang實(shí)現(xiàn)上傳微信頭像功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-10-10
  • 基于vue 實(shí)現(xiàn)表單中password輸入的顯示與隱藏功能

    基于vue 實(shí)現(xiàn)表單中password輸入的顯示與隱藏功能

    這篇文章主要介紹了vue 實(shí)現(xiàn)表單中password輸入的顯示與隱藏功能 ,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-07-07
  • 基于Vue2.X的路由和鉤子函數(shù)詳解

    基于Vue2.X的路由和鉤子函數(shù)詳解

    下面小編就為大家分享一篇基于Vue2.X的路由和鉤子函數(shù)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • 詳解vue項(xiàng)目構(gòu)建與實(shí)戰(zhàn)

    詳解vue項(xiàng)目構(gòu)建與實(shí)戰(zhàn)

    這篇文章主要介紹了詳解vue項(xiàng)目構(gòu)建與實(shí)戰(zhàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • vue中的echarts實(shí)現(xiàn)寬度自適應(yīng)的解決方案

    vue中的echarts實(shí)現(xiàn)寬度自適應(yīng)的解決方案

    這篇文章主要介紹了vue中的echarts實(shí)現(xiàn)寬度自適應(yīng),本文給大家分享實(shí)現(xiàn)方案,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-09-09
  • 一文快速詳解前端框架 Vue 最強(qiáng)大的功能

    一文快速詳解前端框架 Vue 最強(qiáng)大的功能

    組件是 vue.js最強(qiáng)大的功能之一,而組件實(shí)例的作用域是相互獨(dú)立的,這就意味著不同組件之間的數(shù)據(jù)無(wú)法相互引用。這篇文章主要介紹了一文快速詳解前端框架 Vue 最強(qiáng)大的功能,需要的朋友可以參考下
    2019-05-05
  • vue如何通過(guò)params和query傳值(刷新不丟失)

    vue如何通過(guò)params和query傳值(刷新不丟失)

    這篇文章主要介紹了vue如何通過(guò)params和query傳值(刷新不丟失),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 解決使用vue.js路由后失效的問(wèn)題

    解決使用vue.js路由后失效的問(wèn)題

    下面小編就為大家分享一篇解決使用vue.js路由后失效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03

最新評(píng)論

建始县| 弥勒县| 祁阳县| 舒城县| 准格尔旗| 仲巴县| 滨海县| 南溪县| 桦川县| 拉孜县| 西盟| 焦作市| 特克斯县| 微山县| 太仓市| 河南省| 沙洋县| 怀化市| 蓬莱市| 海城市| 大洼县| 隆安县| 镇远县| 铜梁县| 师宗县| 眉山市| 新郑市| 双鸭山市| 镇赉县| 大同市| 鄯善县| 通化市| 贞丰县| 曲松县| 贺州市| 南康市| 蛟河市| 海晏县| 双城市| 涡阳县| 洛川县|