vuex-module-decorators icon indicating copy to clipboard operation
vuex-module-decorators copied to clipboard

Error when using preserveState: true

Open pancake-boy opened this issue 4 years ago • 6 comments

When i call submitQuote() -> which calls the mutation setQuoteResult(); 'this' is undefined.

But it works when i remove preserveState: true

any ideas?

import {
  VuexModule, Module, Mutation, Action, getModule,
} from 'vuex-module-decorators';
import { Inject } from 'inversify-props';
import { Quote, QuoteRequest, QuoteResult } from '@/models/quote';
import store from '@/store';
import TYPES from '@/types';
import IApplicationService from '@/services/IApplicationService';

@Module({ dynamic: true, store, name: 'quote', namespaced: true, preserveState: true })
export class QuoteModule extends VuexModule {
  @Inject(TYPES.ApplicationService)
  private applicationService!: IApplicationService;

  quoteError: string = '';

  quote: Quote = new Quote();

  quoteResult: QuoteResult = new QuoteResult();

  @Mutation
  public setQuoteError(error: string): void {
    this.quoteError = error;
  }

  @Mutation
  public setQuote(quote: Quote): void {
    this.quote = quote;
  }

  @Mutation
  public setQuoteResult(result: QuoteResult): void {
    // this is undefined
    this.quoteResult = result;
  }

  @Action
  public async fetchQuote(quoteId: string): Promise<Quote> {
    try {
      const result = await this.applicationService.getQuote(quoteId);
      this.setQuote(result);
      return result;
    } catch (error) {
      this.setQuoteError(error);
    }

    return new Quote();
  }

  @Action({ rawError: true })
  public async submitQuote(request: QuoteRequest): Promise<QuoteResult> {
    try {
      const result = await this.applicationService.submitQuote(request);
      // when calling this mutation
      this.setQuoteResult(result);
      return result;
    } catch (error) {
      this.setQuoteError(error);
    }

    return new QuoteResult();
  }
}

export default getModule(QuoteModule);
export default new Vuex.Store({
  strict: true,
});

pancake-boy avatar Mar 04 '20 04:03 pancake-boy

I have the same issue.

loferreiranuno avatar May 16 '20 06:05 loferreiranuno

Same issue here, found a solution?

Epenance avatar Jun 09 '20 20:06 Epenance

Guys, be sure that you have correctly set index.ts. For example:

import Vue from 'vue';
import Vuex from 'vuex'
import VuexPersistence from 'vuex-persist';

Vue.use(Vuex);
const vuexLocal = new VuexPersistence({
  storage: window.localStorage,
});

export default new Vuex.Store({
  state: {},
  plugins: [vuexLocal.plugin],
});

and module like this, when it doesn't have a initial state and you load the state from the localStorage @Module({ dynamic: true, store: store, name: 'mm', preserveState: localStorage.getItem('vuex') !== null })

davidsmej88 avatar Jul 08 '20 08:07 davidsmej88

Did you solve it? I have the same issue. And I try to add localStorage.getItem('vuex') !== null }, but it not working

monicana avatar May 11 '21 07:05 monicana

i don't use this lib anymore. maybe easier to just store what u need manually

pancake-boy avatar May 11 '21 11:05 pancake-boy

same issue

DDSameera avatar Apr 17 '23 22:04 DDSameera