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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extends VuexModule fails #30

Closed
sneko opened this issue Oct 2, 2018 · 5 comments
Closed

Extends VuexModule fails #30

sneko opened this issue Oct 2, 2018 · 5 comments
Labels
question Further information is requested

Comments

@sneko
Copy link

sneko commented Oct 2, 2018

Hi @championswimmer 馃槂

I'm putting logger and api client instances in my global state because I want to use them in almost all my store modules.

To avoid using each time this.rootState.logger.debug('blabla') I'm trying to make all my modules herited from a EnrichedVuexModule as follow:

export class EnrichedVuexModule extends VuexModule {
    public test(): string {
        return "heyyyy";
    }

    get $api() {
        return store.state.apiClient;
    }

    get $log() {
        return store.state.logger;
    }
};

And my modules are defined with the following:

@Module({ dynamic: true, store, name: 'authentication' })
export class Authentication extends EnrichedVuexModule {

My TypeScript "linter" detects that this.$api and this.$log exist due to the inheritance but at runtime this.$api and this.$log are not defined (while I can reach them through this.rootState.XXX). Same for the test() function.

Since I'm using dynamic modules and that some stuff is made with decorators by your package, do you know what could cause this trouble please?

Thank you,

@sneko
Copy link
Author

sneko commented Oct 2, 2018

BTW, this.rootState is only accessible at runtime and is not defined while coding the module. Is there an official way to access it without doing (this as any).rootState?

@championswimmer
Copy link
Owner

The rootState can be added to the type defintion. Double inheritance is something I didn't keep in mind when working this out. I neeed to change how the decorator works for that to happen, I'll take a look at this.

@championswimmer championswimmer added the question Further information is requested label Oct 3, 2018
@sneko
Copy link
Author

sneko commented Oct 3, 2018

@championswimmer or maybe do you have another suggestion to implement shared variables between my components that would fit better the Vuex pattern?

I could create a "rootModule" that I would inject in another module through getModule(). But I would still be required to pass through an intermediate variable (utils in this example):

@Module({ dynamic: true, store, name: 'authentication' })
export class Authentication extends VuexModule {
  protected utils = getModule(RootModule.prototype);

  @Action
	public async aaaaa() {
		return this.utils.api.callSomething();

@championswimmer
Copy link
Owner

mixin-type usage in vuex modules were never part of the design, even in default js-based vuex usage. if you want common features, create a separate common module for that maybe

@sneko
Copy link
Author

sneko commented Oct 3, 2018

I've just implemented a module and I need to declare variables outside (and before) the module definiton since all the this.PROPERTY are not kept at runtime:

const { $api, $log } = getModule(Utilities.prototype).object;

@Module({ dynamic: true, store, name: 'authentication' })
export class Authentication extends VuexModule {

I'm not confident about the cleanliness of this way of doing but... it works 馃槃

Thank you for your answers,

@sneko sneko closed this as completed Oct 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants