zoukankan      html  css  js  c++  java
  • vuex 中关于 mapActions 的作用

      mapActions 工具函数会将 store 中的 dispatch 方法映射到组件的 methods 中。和 mapState、mapGetters 也类似,只不过它映射的地方不是计算属性,而是组件的 methods 对象上。我们来直接看它的实现:

    export function mapActions (actions) {
    	const res = {}
    	normalizeMap(actions).forEach(({ key, val }) => {
    		res[key] = function mappedAction (...args) {
    			return this.$store.dispatch.apply(this.$store, [val].concat(args))
    		}
    	})
    	return res
    }
    

      可以看到,函数的实现套路和 mapState、mapGetters 差不多,甚至更简单一些,实际上就是做了一层函数包装。为了更直观地理解,我们来看一个简单的例子:

    import { mapActions } from 'vuex'
    export default {
    	// ...
    	methods: {
    		...mapActions([
    			'increment' // 映射 this.increment() 到 this.$store.dispatch('increment')
    		]),
    		...mapActions({
    			add: 'increment' // 映射 this.add() to this.$store.dispatch('increment')
    		})
    	}
    }
    

      经过 mapActions 函数调用后的结果,如下所示:

    import { mapActions } from 'vuex'
    export default {
    	// ...
    	methods: {
    		increment(...args) {
    			return this.$store.dispatch.apply(this.$store, ['increment'].concat(args))
    		}
    		add(...args) {
    			return this.$store.dispatch.apply(this.$store, ['increment'].concat(args))
    		}
    	}
    }
    

    .

  • 相关阅读:
    团队任务拆解
    团队贡献分分配规则
    使用highcharts绘制美观的燃尽图
    【Alpha】阶段第一次Scrum Meeting
    项目功能规格说明书
    技术规格说明书
    软件工程团队第二次作业
    软工第一次团队作业
    团队作业 # 项目功能规格说明书
    团队作业 #2 —— 项目选择
  • 原文地址:https://www.cnblogs.com/crazycode2/p/7636086.html
Copyright © 2011-2022 走看看