多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
![](https://img.kancloud.cn/83/f9/83f9a16ac222b49c1f2f6ee19d89cc4f_966x504.png) 为了简便起见,Vuex 提供了四个方法用来方便的将这些功能结合进组件。 1. `mapState` 2. `mapGetters` 3. `mapMutations` 4. `mapActions` 示例代码: ~~~ import { mapState, mapGetters, mapMutations, mapActions } from 'vuex' // .... computed: { localComputed () { /* ... */ }, ...mapState({ // 为了能够使用 `this` 获取局部状态,必须使用常规函数 count(state) { return state.count + this.localCount } }), ...mapGetters({ getterCount(state, getters) { return state.count + this.localCount } }) } methods: { ...mapMutations({ add: 'increment' // 将 `this.add()` 映射为`this.$store.commit('increment')` }), ...mapActions({ add: 'increment' // 将 `this.add()` 映射为 `this.$store.dispatch('increment')` }) } ~~~ 如果结合进组件之后不想改变名字,可以直接使用**数组**的方式。 ~~~ methods: { ...mapActions([ 'increment', // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')` // `mapActions` 也支持载荷: 'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)` ]), } ~~~