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

Dependency injection of dependend vuexmodules via getModule #208

Open Chris2011 opened 4 years ago

Chris2011 commented 4 years ago

Hi,

I use vuex with TS and your package to use the decorators and the getModule method.

Now I have a store with a ctor, which needs 2 more modules as dependencies. Atm I do this in the ctor of my module

@Module({
    dynamic: true,
    name: 'MyStore',
    store,
    namespaced: true,
})
export default class MyStore extends VuexModule {
    private sessionStore: SessionStore;
    private notificationStore: NotificationStore;

    constructor(module: Mod<ThisType<{}>, any>) {
        super(module);

        this.sessionStore = getModule(SessionStore);
        this.notificationStore = getModule(NotificationStore);
    }
    ...
}

The problem here is while testing, I need to inject them in another way, prefered would be via constructor. Is there a way, getModule can handle this or how do I do this?

I prefer this constructor(module: Mod<ThisType<{}>, any>, sessionStore: SessionStore, notificationStore: NotificationStore) {

Cheers

Chris

tatemz commented 4 years ago

I have a related question similar to @Chris2011's. Vanilla Vuex mutations take state as a parameter. This is great for testing because I can mock the state that will be used in the mutation.

When using a class based approach, a constructor would be the way to go, I suppose. How do we do this using this library?