vuex中的輔助函數(shù)基本的使用

mapState

import { mapState } from 'vuex'

export default {

? // ...

? computed:{

? ? ...mapState({

? ? ? ? // 箭頭函數(shù)可使代碼更簡(jiǎn)練

? ? ? ? count: state => state.count,

? ? ? ? // 傳字符串參數(shù) 'count' 等同于 `state => state.count`

? ? ? ? countAlias: 'count',

? ? ? ? // 為了能夠使用 `this` 獲取局部狀態(tài),必須使用常規(guī)函數(shù)

? ? ? ? countPlusLocalState (state) {

? ? ? ? ? ? return state.count + this.localCount

? ? ? ? }

? })

? }

}

定義的屬性名與state中的名稱(chēng)相同時(shí),可以傳入一個(gè)數(shù)組

//定義state

const state={

? ? count:1,

}

//在組件中使用輔助函數(shù)

computed:{

? ? ...mapState(['count'])

}

mapGetters

computed:{

? ? ...mapGetters({

? ? ? // 把 `this.doneCount` 映射為 `this.$store.getters.doneTodosCount`

? ? ? doneCount: 'doneTodosCount'

? ? })

}

當(dāng)屬性名與getters中定義的相同時(shí),可以傳入一個(gè)數(shù)組

computed:{

? computed: {

? // 使用對(duì)象展開(kāi)運(yùn)算符將 getter 混入 computed 對(duì)象中

? ? ...mapGetters([

? ? ? 'doneTodosCount',

? ? ? 'anotherGetter',

? ? ? // ...

? ? ])

? }

}

總結(jié):

mapState與mapGetters都用computed來(lái)進(jìn)行映射

在組件中映射完成后,通過(guò)this.映射屬性名進(jìn)行使用

mapMutations

methods:{

? ? ...mapMutations({

? ? ? ? add: 'increment' // 將 `this.add()` 映射為 `this.$store.commit('increment')`

? ? })

}

當(dāng)屬性名與mapMutatios中定義的相同時(shí),可以傳入一個(gè)數(shù)組

methods:{

? ? ...mapMutations([

? ? ? ? 'increment', // 將 `this.increment()` 映射為 `this.$store.commit('increment')`

? ? ? ? // `mapMutations` 也支持載荷:

? ? ? ? 'incrementBy' // 將 `this.incrementBy(amount)` 映射為 `this.$store.commit('incrementBy', amount)`

? ? ]),

}

mapActios

mathods:{

? ? ...mapActions({

? ? ? ? add: 'increment' // 將 `this.add()` 映射為 `this.$store.dispatch('increment')`

? ? })

}

當(dāng)屬性名與mapActios中定義的相同時(shí),可以傳入一個(gè)數(shù)組

methods:{

? ? ...mapActions([

? ? ? ? 'increment', // 將 `this.increment()` 映射為 `this.$store.dispatch('increment')`

? ? ? ? // `mapActions` 也支持載荷:

? ? ? ? 'incrementBy' // 將 `this.incrementBy(amount)` 映射為 `this.$store.dispatch('incrementBy', amount)`

? ? ]),

}

總結(jié)

mapMutations與mapActios都在methods中進(jìn)行映射

映射之后變成一個(gè)方法

多個(gè)modules

在不使用輔助函數(shù)的時(shí)候,

this.$store.commit('app/addCount')

使用輔助函數(shù),輔助函數(shù)的第一個(gè)參數(shù)給定命名空間的路徑

computed: {

? ...mapState('some/nested/module', {

? ? a: state => state.a,

? ? b: state => state.b

? })

},

methods: {

? ...mapActions('some/nested/module', [

? ? 'foo', // -> this.foo()

? ? 'bar' // -> this.bar()

? ])

}

或者使用 createNamespacedHelpers函數(shù)來(lái)創(chuàng)建一個(gè)基于命名空間的輔助函數(shù)

import { createNamespacedHelpers } from 'vuex'

const { mapState, mapActions } = createNamespacedHelpers('some/nested/module') //給定路徑

//使用方法與之前相同

export default {

? computed: {

? ? // 在 `some/nested/module` 中查找

? ? ...mapState({

? ? ? a: state => state.a,

? ? ? b: state => state.b

? ? })

? },

? methods: {

? ? // 在 `some/nested/module` 中查找

? ? ...mapActions([

? ? ? 'foo',

? ? ? 'bar'

? ? ])

? }

}

?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • ### store 1. Vue 組件中獲得 Vuex 狀態(tài) ```js //方式一 全局引入單例類(lèi) // 創(chuàng)建一...
    蕓豆_6a86閱讀 783評(píng)論 0 3
  • Vuex 的學(xué)習(xí)記錄 資料參考網(wǎng)址Vuex中文官網(wǎng)Vuex項(xiàng)目結(jié)構(gòu)示例 -- 購(gòu)物車(chē)Vuex 通俗版教程N(yùn)uxt....
    流云012閱讀 1,537評(píng)論 0 7
  • 安裝 npm npm install vuex --save 在一個(gè)模塊化的打包系統(tǒng)中,您必須顯式地通過(guò)Vue.u...
    蕭玄辭閱讀 3,035評(píng)論 0 7
  • ### store 1. Vue 組件中獲得 Vuex 狀態(tài) ```js //方式一 全局引入單例類(lèi) // 創(chuàng)建一...
    蕓豆_6a86閱讀 438評(píng)論 0 0
  • vue是當(dāng)今比較火的一種框架,在很多方面都會(huì)用到Vue,今天帶大家簡(jiǎn)單了解一下vue中一款常用的插件VueX 首先...
    大馬虎閱讀 352評(píng)論 0 0

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