是否可以将 ES6 类 用于 Vuex 模块、突变和操作?
Is it OK to use ES6 classes for Vuex modules, mutations and actions?
我将以这种方式创建可重用的 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 操作并在必要时为某些模块扩展它们的方式。
所有模块都将使用命名空间。
有没有潜在的问题?
这可能是可能的,但当您只需要对象时使用 类 似乎不必要地复杂。
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
}
我将以这种方式创建可重用的 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 操作并在必要时为某些模块扩展它们的方式。
所有模块都将使用命名空间。
有没有潜在的问题?
这可能是可能的,但当您只需要对象时使用 类 似乎不必要地复杂。
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
}