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 use a constructor in a module? #105

Closed
MagnusJohansson opened this issue Mar 17, 2019 · 6 comments
Closed

How can I use a constructor in a module? #105

MagnusJohansson opened this issue Mar 17, 2019 · 6 comments

Comments

@MagnusJohansson
Copy link

I need to do some initialization work whenever the module is constructed.
But when I add the super(); call, it expects a module parameter.
What should I pass in there?

@Module
export default class RealTime extends VuexModule {
    private playersOnline!: [];

    constructor() {
        super();
    }

    get getPlayersOnline() {
        return this.playersOnline;
    }
}; 

constructor VuexModule<ThisType, any>(module: Module<ThisType, any>): VuexModule<ThisType, any>
Expected 1 arguments, but got 0.ts(2554)
vuexmodule.d.ts(16, 17): An argument for 'module' was not provided.

@championswimmer
Copy link
Owner

championswimmer commented Mar 18, 2019 via email

@Botvinkin
Copy link

Modules are not supposed to be constructed. Use getModule() to fetch a module object.

This prevents me from using DI (inversify.js) with modules.

@spremotely
Copy link

Modules are not supposed to be constructed. Use getModule() to fetch a module object.

This prevents me from using DI (inversify.js) with modules.

It's wonderfull thing but look - if you want to inject something (this real cool to inject service to module and use it in MutationAction) - you can't

@douglasg14b
Copy link

douglasg14b commented Jun 20, 2020

And if you need to get a module, but for some reason can't because of an unresolved issue #266 ?

I need to get my authStore, but can't use getModule() since it seems to break the webpack output.... The workaround would be to call my static class from the constructor and pass the module as an argument to get it where I need it for now.

@chriso86
Copy link

Modules are not supposed to be constructed. Use getModule() to fetch a module object.

This prevents me from using DI (inversify.js) with modules.

It's wonderfull thing but look - if you want to inject something (this real cool to inject service to module and use it in MutationAction) - you can't

This makes me sad.

@Moongazer
Copy link

What if we want to use a store variable of type Map or Set? The variable must by initialized with new, which IMO has to happen in a constructor. At least it does not work if the new statement is written in the declaration:

class ApplicationModule extends VuexModule {
  private mySet: Set<number> = new Set<number>()
  // ...
}

Accessing the mySet state from somewhere in the application always throws "get() or has() is not a function", which I think is because the state was never initialized as such.

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

7 participants