Appearance
在组件中分发 Action
本节目标:理解“在组件中分发 Action”的核心思路,并能把它用于实际开发或面试表达。 你在组件中使用
this.$store.dispatch('xxx')分发action,或者使用mapActions辅助函数将组件的methods映射为store.dispatch调用(需要先在根节点注入store):
import { mapActions } from 'vuex'
export default { // ... methods: { ...mapActions([ 'increment', //将 `this.increment()` 映射为
`this.$store.dispatch('increment')`
// `mapActions`也支持载荷:
'incrementBy' //将 `this.incrementBy(amount)` 映射为
`this.$store.dispatch('incrementBy', amount)` ]), ...mapActions({ add: 'increment' //将 `this.add()` 映射为
`this.$store.dispatch('increment')` }) }}