亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

相關(guān)文章

  • 詳解Vue-Cli 異步加載數(shù)據(jù)的一些注意點(diǎn)

    詳解Vue-Cli 異步加載數(shù)據(jù)的一些注意點(diǎn)

    本篇文章主要介紹了詳解Vue-Cli 異步加載數(shù)據(jù)的一些注意點(diǎn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • Vue中的路由導(dǎo)航守衛(wèi)導(dǎo)航解析流程

    Vue中的路由導(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-04
  • Vue2.x響應(yīng)式簡(jiǎn)單講解及示例

    Vue2.x響應(yīng)式簡(jiǎn)單講解及示例

    這篇文章主要介紹了Vue2.x響應(yīng)式及簡(jiǎn)單的示例,應(yīng)用了簡(jiǎn)單的源代碼進(jìn)行講解,感興趣的小伙伴可以參考一下,希望可以幫助到你
    2021-08-08
  • vue-cli axios請(qǐng)求方式及跨域處理問(wèn)題

    vue-cli axios請(qǐng)求方式及跨域處理問(wèn)題

    這篇文章主要介紹了vue-cli axios請(qǐng)求方式及跨域處理問(wèn)題,文中還給大家提到了vue中axios解決跨域問(wèn)題和攔截器使用,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2018-03-03
  • vue輪詢請(qǐng)求解決方案的完整實(shí)例

    vue輪詢請(qǐng)求解決方案的完整實(shí)例

    項(xiàng)目開發(fā)中需要做一個(gè)輪詢,所以將實(shí)現(xiàn)的過(guò)程記錄了一下,下面這篇文章主要給大家介紹了關(guān)于vue輪詢解決方案的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • vue中watch監(jiān)聽不到變化的解決

    vue中watch監(jiān)聽不到變化的解決

    本文主要介紹了vue中watch監(jiān)聽不到變化的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Vue 3.0中Treeshaking特性及作用

    Vue 3.0中Treeshaking特性及作用

    Tree shaking 是一種通過(guò)清除多余代碼方式來(lái)優(yōu)化項(xiàng)目打包體積的技術(shù),就是在保持代碼運(yùn)行結(jié)果不變的前提下,去除無(wú)用的代碼,本文給大家介紹Vue 3.0中Treeshaking特性是什么,感興趣的朋友一起看看吧
    2023-10-10
  • Vue入門之?dāng)?shù)據(jù)綁定(小結(jié))

    Vue入門之?dāng)?shù)據(jù)綁定(小結(jié))

    本篇文章主要介紹了探索Vue高階組件的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • Vue如何整合mavon-editor編輯器(markdown編輯和預(yù)覽)

    Vue如何整合mavon-editor編輯器(markdown編輯和預(yù)覽)

    這篇文章主要介紹了Vue整合mavon-editor編輯器(markdown編輯和預(yù)覽)的相關(guān)知識(shí),mavon-editor是目前比較主流的markdown編輯器,重點(diǎn)介紹它的使用方法,需要的朋友可以參考下
    2022-10-10
  • Vue模板語(yǔ)法v-bind教程示例

    Vue模板語(yǔ)法v-bind教程示例

    這篇文章主要為大家介紹了Vue模板語(yǔ)法v-bind教程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12

最新評(píng)論