Vuex-module-decorators: Cannot call methods in getter?

Created on 1 Jun 2019  路  1Comment  路  Source: championswimmer/vuex-module-decorators

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...

Most helpful comment

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

>All comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TotomInc picture TotomInc  路  7Comments

davidhiendl picture davidhiendl  路  3Comments

tuvokki picture tuvokki  路  7Comments

stevefan1999-personal picture stevefan1999-personal  路  5Comments

blackmad picture blackmad  路  4Comments