Vuex 惯用点

5 阅读1分钟

1. 模块化

这两种写法的很容易疏忽区别,映射后的调用方法名称改变了,要注意名称区别,在做项目中接手别人的代码是可能会比较容易疏忽。(mapState,mapMutations同理)

  ...mapActions([
    'some/nested/module/foo', // -> this['some/nested/module/foo']()
    'some/nested/module/bar' // -> this['some/nested/module/bar']()
  ])
...mapActions('some/nested/module', [
    'foo', // -> this.foo()
    'bar' // -> this.bar()
])

2. action

mutation 只允许同步

action 允许异步操作,且可以返回 promise 进行链式调用

3. 命名空间

namespaced: true 这里可别写错了,有个 d

引入命名空间主要是为了解决 mutation actions 重名的问题,也是为了代码结构更加清晰,分类明确