I'm trying to call methods in getter, but console says this.<the_method_name> is not a function.
I wrote a code like:
@Module({ name: 'example', stateFactory: true, namespaced: true })
class Example extends VuexModule {
a = 3;
b = 5;
getter sum(): number {
return this.calc();
}
private calc(): number {
return this.a + this.b;
}
}
This code will return this.calc() is not a function.
I use vuex-module-decorators in Nuxt.
P.S.
Sorry for my poor English...
From reading the documentation I get that none of the methods or run with this as a class instance. See the docs for @mutation and @action.
So what you need to do is to write some functions outside the class:
function sum(a, b) {
return a+b;
}
@Module({ name: 'example', stateFactory: true, namespaced: true })
class Example extends VuexModule {
a = 3;
b = 5;
getter sum(): number {
return sum(this.a,this.b);
}
}
Should work, but I haven't tested it.
PS: This package makes (private methods accessible) to getters, mutations and actions: https://github.com/gertqin/vuex-class-modules
Most helpful comment
From reading the documentation I get that none of the methods or run with
thisas a class instance. See the docs for@mutationand@action.So what you need to do is to write some functions outside the class:
Should work, but I haven't tested it.
PS: This package makes (private methods accessible) to getters, mutations and actions: https://github.com/gertqin/vuex-class-modules