Store.mapActions

创建组件方法分发 action。详细介绍

参数说明
参数类型必填说明
mapArray | Object如果是对象形式,成员可以是一个函数。function(dispatch: function, …args: any[])
示例
  1. // store.js
  2. import createStore from 'chameleon-store'
  3. const store = createStore({
  4. state: {
  5. count: 0
  6. },
  7. mutations: {
  8. increment (state) {
  9. state.count++
  10. }
  11. },
  12. actions: {
  13. increment (context) {
  14. context.commit('increment')
  15. }
  16. }
  17. })
  18. export default store
  19. // app.js
  20. import store from './store.js'
  21. class Index {
  22. // ...
  23. methods = {
  24. ...store.mapActions([
  25. 'increment', // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')`
  26. ]),
  27. ...store.mapActions({
  28. add: 'increment' // 将 `this.add()` 映射为 `this.$store.dispatch('increment')`
  29. })
  30. }
  31. };
  32. export default new Index();