Search code examples
javascripttypescriptvue.jsvue-componentvuex

Typescript Vuex - How to define a state with a setter and getter?


In the vuex store I want to initialize a state called _token. When I'm trying to access the property within the same class, it gives me the error message that the setter for _token is not defined. Anyone has an idea why only the getter is available?

export default class Api extends VuexModule {
  public _token = '';

  @Action
  public async [VuexActionsApi.VerifyUser](data: any): Promise<IResponseState> {
    const vuexInstance = this;

    // this is not working
    vuexInstance._token = "test";
}

log output


Solution

  • It seems to work now, when i defined the state like this:

    public data = {
      bearerToken: undefined
    };
    

    Now the setter and getter functions are created correctly. Someone has an idea why the first approach didn't work?