vuex-smart-module icon indicating copy to clipboard operation
vuex-smart-module copied to clipboard

Can't get context of nested cloned module

Open Dodje opened this issue 3 years ago • 0 comments

Hi, I have a reusable module, which I wish to use as submodule in other different modules.

class ReusableState {
    data = {};
}

const reusableNested = new Module({
    namespaced: false,
    state: ReusableState,
});

// ...
class MainModuleActions extends Actions<
    MainModuleState,
    MainModuleGetters,
    MainModuleMutations,
    MainModuleActions
> {
    private nested!: Context<typeof reusableNested>;

    $init(store: Store<any>): void {
        this.nested = reusableNested.context(store);
    }

    getNestedState(): void {
        console.log(this.nested.state);
    }
}

const mainModule = new Module({
    namespaced: true,
    state: MainModuleState,
    getters: MainModuleGetters,
    mutations: MainModuleMutations,
    actions: MainModuleActions,
    modules: {
        nested: reusableNested.clone(),
    },
});

When I call getNestedState I have a message:

Error: [vuex-smart-module] The module need to be registered a store before using `Module#context` or `createMapper`
    at assert (vuex-smart-module.esm.js?675f:165)
    at Object.get path [as path] (vuex-smart-module.esm.js?675f:201)
    at Context.get (vuex-smart-module.esm.js?675f:300)

And that happens only if nested module is cloned, otherwise all works fine

Version 0.5.0

Dodje avatar Oct 19 '21 11:10 Dodje