question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when using preserveState: true

See original GitHub issue

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,
});

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

8reactions
davidsmej88commented, Jul 8, 2020

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 })

0reactions
pancake-boycommented, May 11, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Manual visits - Inertia.js
The reload() method is simply a shorthand that automatically visits the current page, with preserveState and preserveScroll both set to true.
Read more >
inertia get clearing my search input type - vue.js - Stack Overflow
Try using the { preserveState:true } option. Your code would like something like this: this.$inertia.get("/member", { term: this.term } ...
Read more >
API Reference | Vuex
options can have preserveState: true that allows to preserve the previous state. Useful with Server Side Rendering.
Read more >
@inertiajs/inertia-vue NPM | npm.io
Check @inertiajs/inertia-vue 0.8.0 package - Last release 0.8.0 with MIT licence ... preserveScroll: false, preserveState: true }) $inertia.put(url, data, ...
Read more >
дэн on Twitter: "@glenathan The error boundaries won't preserve ...
That'll be the other major transform use-case ... The error boundaries won't preserve state though, I might need to put them right into...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found