vuex--輔助函數(shù)mapState,mapGetters, mapActions, mapMutations

  • 以mapActions為例,通常,我們?cè)趘uex的actions中定義一個(gè)方法:
export default {
    vuexSetUserLogin({commit}){
        commit("vuex_set_user_login");
    },
    vuexSetTest({commit}, val){
        commit("vuex_set_test", val);
    }
}

調(diào)用時(shí)這樣調(diào)用:

this.$store.dispatch("vuexSetTest", "ttttttt");
  • mapActions的作用就是,把a(bǔ)ctions中的方法映射到methods中,換一種調(diào)用方式。
    引入mapAction:
import {mapActions,} from 'vuex'

映射方法:

"methods": {
    ...mapActions([
        "vuexSetUserLogin",
        "vuexSetTest"
    ]),
},

把a(bǔ)ctions里面的兩個(gè)方法映射到本地的同名方法
調(diào)用的時(shí)候就像調(diào)用本組件的method一樣:

this.vuexSetTest("eeee");
  • mapActions() 返回的是一個(gè)對(duì)象, 用了 ... 擴(kuò)展符后,才可以放進(jìn)一個(gè)對(duì)象里,和其他組件內(nèi)定義的 method 在同一個(gè) methods 對(duì)象。
{
    methods: mapActions() // 如果沒有其它組件內(nèi)的定義的方法,可以這樣寫
}
{
    methods: {
        ...mapActions(),// 如果有其他定義的方法
        otherMethod1 () {},
        otherMethod2 () {}
    }
}
  • mapGetters:
computed:mapGetters([
 'count'
 ]),
  • 映射的時(shí)候可以取別名:
export default {
  // ...
  methods: {
       //下述中的 ... 是拓展運(yùn)算符
       // 使用 [] 是解構(gòu)賦值
    ...mapActions([
      'increment', // 將 `this.increment()` 映射為 `this.$store.dispatch('increment')`
 
      // `mapActions` 也支持載荷:
      'incrementBy' // 將 `this.incrementBy(amount)` 映射為 `this.$store.dispatch('incrementBy', amount)`
    ]),
    ...mapActions({
      add: 'increment' // 將 `this.add()` 映射為 `this.$store.dispatch('increment')`
    })
  }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 配置問題 瀏覽器只支持JavaScript語(yǔ)法,所以我們需要在項(xiàng)目中配置babel,在打包的時(shí)候進(jìn)行轉(zhuǎn)換,否則會(huì)報(bào)...
    Kiki_Q閱讀 1,490評(píng)論 0 0
  • ### store 1. Vue 組件中獲得 Vuex 狀態(tài) ```js //方式一 全局引入單例類 // 創(chuàng)建一...
    蕓豆_6a86閱讀 786評(píng)論 0 3
  • 安裝 npm npm install vuex --save 在一個(gè)模塊化的打包系統(tǒng)中,您必須顯式地通過Vue.u...
    蕭玄辭閱讀 3,037評(píng)論 0 7
  • 目錄 - 1.什么是vuex? - 2.為什么要使用Vuex? - 3.vuex的核心概念?||如何在組件中去使用...
    我跟你蔣閱讀 4,236評(píng)論 4 51
  • ### store 1. Vue 組件中獲得 Vuex 狀態(tài) ```js //方式一 全局引入單例類 // 創(chuàng)建一...
    蕓豆_6a86閱讀 443評(píng)論 0 0

友情鏈接更多精彩內(nèi)容