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' ]) } }
以上就是vuex的輔助函數(shù)該如何使用的詳細(xì)內(nèi)容,更多關(guān)于vuex的輔助函數(shù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue 報(bào)錯(cuò)Error: No PostCSS Config foun
這篇文章主要介紹了Vue 報(bào)錯(cuò)Error: No PostCSS Config found問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07Vue.js中vue-property-decorator的使用方法詳解
vue-property-decorator是一個(gè)用于在Vue.js中使用TypeScript裝飾器的庫(kù),它能夠簡(jiǎn)化 Vue 組件的定義,使代碼更加簡(jiǎn)潔和可維護(hù),它能夠簡(jiǎn)化Vue組件的定義,使代碼更加簡(jiǎn)潔和可維護(hù),本文將深入探討vue-property-decorator的使用方法,并展示如何在Vue.js項(xiàng)目中應(yīng)用它2024-08-08vue.js移動(dòng)端app之上拉加載以及下拉刷新實(shí)戰(zhàn)
這篇文章主要介紹了vue.js移動(dòng)端app之上拉加載以及下拉刷新實(shí)戰(zhàn),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09vue實(shí)現(xiàn)點(diǎn)擊展開(kāi)點(diǎn)擊收起效果
這篇文章主要介紹了vue實(shí)現(xiàn)點(diǎn)擊展開(kāi),點(diǎn)擊收起效果,首先我們需要定義data里面的數(shù)據(jù),使用computed對(duì)data進(jìn)行處理,需要的朋友可以參考下2018-04-04關(guān)于Element-UI Table 表格指定列添加點(diǎn)擊事件
這篇文章主要介紹了關(guān)于Element-UI Table 表格指定列添加點(diǎn)擊事件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09vue實(shí)現(xiàn)簡(jiǎn)單表格組件實(shí)例詳解
vue的核心思想就是組件,什么是組件呢?按照我的理解組件就是裝配頁(yè)面的零件,vue三大核心組件 路由 狀態(tài)管理,路由控制頁(yè)面的渲染,頁(yè)面由組件組成,數(shù)據(jù)有vuex進(jìn)行管理和改變。下面我會(huì)以一個(gè)簡(jiǎn)單的案例來(lái)說(shuō)2017-04-04