Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lixingjuan opened this issue Mar 25, 2021 · 4 comments
Open

Comments

@lixingjuan
Copy link

No description provided.

@lixingjuan
Copy link
Author

`
@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
Copy link

// 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
Copy link

Murali-codes commented Aug 15, 2021

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
Copy link

steven87vt commented Apr 14, 2022

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:

const storeModule = genStatic(store)

defined:

Object.defineProperty(constructor, '_genStatic', {

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)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants