vuex 中关于 mapMutations 的作用
2017-10-08 18:00
495 查看
mapMutations 工具函数会将 store 中的 commit 方法映射到组件的 methods 中。和 mapActions 的功能几乎一样,我们来直接看它的实现:
函数的实现几乎也和 mapActions 一样,唯一差别就是映射的是 store 的 commit 方法。为了更直观地理解,我们来看一个简单的例子:
经过 mapMutations 函数调用后的结果,如下所示:
.
export function mapMutations (mutations) { const res = {} normalizeMap(mutations).forEach(({ key, val }) => { res[key] = function mappedMutation (...args) { return this.$store.commit.apply(this.$store, [val].concat(args)) } }) return res }
函数的实现几乎也和 mapActions 一样,唯一差别就是映射的是 store 的 commit 方法。为了更直观地理解,我们来看一个简单的例子:
import { mapMutations } from 'vuex' export default { // ... methods: { ...mapMutations([ 'increment' // 映射 this.increment() 到 this.$store.commit('increment') ]), ...mapMutations({ add: 'increment' // 映射 this.add() 到 this.$store.commit('increment') }) } }
经过 mapMutations 函数调用后的结果,如下所示:
import { mapActions } from 'vuex' export default { // ... methods: { increment(...args) { return this.$store.commit.apply(this.$store, ['increment'].concat(args)) } add(...args) { return this.$store.commit.apply(this.$store, ['increment'].concat(args)) } } }
.
相关文章推荐
- vuex 中关于 mapState 的作用
- vuex中关于mapState,mapGetters,mapMutations,mapActions的作用
- vuex 中关于 mapGetters 的作用
- vuex 中关于 mapActions 的作用
- vue关于nextTick方法的作用
- vuex中关于mapState,mapGetters,mapMutations,mapActions的作用
- 关于vue.js中作用域插槽的实例
- vue中 关于$emit的用法
- 关于vue2.0的一些文章
- 关于最近流行的vuejs.angularjs,Reactjs等前端框架的简单对比及体验
- 关于串联匹配电阻其作用
- 关于虚表dual 的来源 作用
- 关于vue2.0工程发布上线操作步骤
- 关于MinGW下.dll.a文件的作用
- Vue------第五天(关于组件的一些基础了解,包括组件的注册、Prop、自定义事件、使用插槽分发内容等)
- 关于vue.js弹窗组件的知识点总结
- 关于vue 框架与后台框架的混合使用的尝试
- vue-router、vue-loader、vue-cli,v-source的作用
- 关于反码,byte最大值127,最小值-128的由来缘由以及书本上从未讲解过的反码补码功能作用
- 关于ServletContextListener的认识以及作用