championswimmer / vuex-module-decorators

TypeScript/ES7 Decorators to create Vuex modules declaratively
https://championswimmer.in/vuex-module-decorators/
MIT License
1.8k stars 170 forks source link

How can I add a submodule for a Module in vuex-module-decorators? #355

Open lixingjuan opened 3 years ago

lixingjuan commented 3 years ago

` @Module({ name: 'Son', dynamic: true, namespaced: true, store, }) class Son extends VuexModule { / 分页信息 当前页 / public pageIndex = 1; }

@Module({ name: 'Father', dynamic: true, namespaced: true, store, }) class Father extends VuexModule { modules = { Son: getModule(Son), }; } `

I want the Son to be Father's Module, but I got the error

llllllllllx commented 3 years ago
// Son.ts
@module({
    name: 'Son',
    namespaced: true,
})
export class Son extends VuexModule {
    public pageIndex = 1;
}
// Father.ts
import Son from 'Son.ts'

@module({
    name: 'Father',
    dynamic: true,
    namespaced: true,
    store,
})
class Father extends VuexModule {
    modules: {
        Son: Son,
    };
}
Murali-codes commented 3 years ago

I came across similar use case recently , below is the way i have implemented

@Module({namespaced: true, name: 'father'})
  class Parent extends VuexModule{}

@Module({namespaced; true, name: 'parent/child'})
class Child extends VuexModule{}

//in .vue component

 $store.registerModule('parent', Parent);
 $store.registerModule(['parent', 'child'], Child)

Not sure if this is the best way , but its the only way i found so far. I didn't try wrt dynamic modules

steven87vt commented 2 years ago

For this to work, the @Module({ name: 'some/path' }) option has to be updated to support an array name: ['some', 'path']. Internally, after this framework hands the module off to Vuex store.registerModule, vuex first converts the string to an array anyways.

The problem comes into play when fetching the module using getModule(any) as it calls genStatic(store) which is not overridable in the decorator handler:

call: https://github.com/championswimmer/vuex-module-decorators/blob/115c24aee0b4cea3d5251f8b4d2a149854c52e37/src/vuexmodule.ts#L64

defined: https://github.com/championswimmer/vuex-module-decorators/blob/115c24aee0b4cea3d5251f8b4d2a149854c52e37/src/module/index.ts#L76

So even though when you force the name: [] array input in the decorator, using // @ts-ignore to get around the compiler error, the dynamic getModule cannot handle an array name type (which the following lines depend on anyways).

@championswimmer hope this issue gets updated in your next release. it might be sufficient to just change the module name option from name: string to name: string[] and handle the paths this way since internally the vuex framework is using [].reduce to isolate modules within the store:

function getNestedState (state, path) {
  return path.reduce(function (state, key) { return state[key]; }, state)
}