【问题标题】:Is it OK to use ES6 classes for Vuex modules, mutations and actions?可以为 Vuex 模块、突变和动作使用 ES6 类吗?
【发布时间】:2019-12-25 11:24:05
【问题描述】:

我将通过这种方式创建可重用的 Vuex 模块:

// parent class to keep all the common CRUD actions
class ListModule {
    state: getInitialState(),
    actions: new ListActions(),
    mutations: new ListMutations()
}

...
class FooMutations extends ListMutations {
    // some additional mutations
}

// class with parent's actions and data structure, but modified mutations
class FooModule extends ListModule {
    mutations: new FooMutations()
}

这就是我希望避免重复 CRUD 操作并在必要时为某些模块扩展它们的方式。

所有模块都将使用命名空间。

有什么潜在的问题吗?

【问题讨论】:

    标签: vue.js ecmascript-6 vuex es6-class


    【解决方案1】:

    这可能是可能的,但当您只需要对象时使用类似乎不必要地复杂。

    const listMutations = {
      // mutations here
    }
    
    // 'extend' listMutations
    const fooMutations = {
      ...listMutations,
      // some additional mutations
    }
    
    const listModule = {
      state: getInitialState(),
      actions: listActions,
      mutations: listMutations
    }
    
    // 'extend' listModule
    const fooModule = {
      ...listModule,
      mutations: fooMutations
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-13
      • 2019-04-15
      • 2021-04-06
      • 2019-03-29
      • 1970-01-01
      • 2020-12-17
      • 2018-10-01
      • 2017-01-10
      相关资源
      最近更新 更多