Persistent data in Vuex is lost upon refresh

Trying to persist data after a page reload can be tricky. I recently came across Vuex as a solution for this issue. While I successfully implemented it in a project using JavaScript, I've been facing difficulties with TypeScript. Despite assigning data before the page refresh, upon reloading, the default values are being used instead.

import Vue from 'vue'
import Vuex from 'vuex'
import VuexPersistence from 'vuex-persist'
import LanguageEntry from '../classes/LanguageEntry'

Vue.use(Vuex)

export interface State {
  IsDark: boolean;
  CurrentLanuage: string;
  LanguageValues: Array<LanguageEntry>;
}

export default new Vuex.Store<State>({
  state: {
    IsDark: false,
    CurrentLanuage: "DE",
    LanguageValues: Array<LanguageEntry>(),
  },
  plugins: [new VuexPersistence().plugin],
})

Answer №1

I recently updated a popular plugin called vuex-persistedstate to persist data after refreshing, and I noticed that it has been updated more recently than the one you are using.

In my /route index.js file, I implement it like this:

import createPersistedState from 'vuex-persistedstate';

export default function (/* { ssrContext } */) {
  const store = new Vuex.Store({
    modules: {
      // example
      clients,
      auth: authentication,
    },
    plugins: [createPersistedState()],
    // enable strict mode (adds overhead!)
    // for dev mode only
    strict: process.env.DEBUGGING
  })

  return store
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to retrieve the input field's name using an Angular2 FormControl object?

My Angular 2 application features the ReactiveForms module for managing a form with its own custom validator. This validator takes in a FormControl object as an input parameter. I've noticed that multiple input fields could benefit from using this sam ...

Certain sections within a Formik form are failing to update as intended

I have successfully implemented a custom TextField wrapper for Material-UI fields, but I am facing an issue with native Material UI fields not updating the form data upon submission. Below is the relevant code snippet along with a link to a code sandbox d ...

Place two buttons next to each other on the same row

Is there a way to align Native Base buttons so they both appear on the same line? Currently, the second button is positioned correctly on the right, but it seems slightly lower than the first button. How can I adjust this alignment? <View sty ...

The Angular reactive form is being submitted without completing the submission process

I've been working on an Angular 5 reactive form and everything seems to be functioning correctly. However, I've encountered a strange issue with a button used to close the form by hiding it from view. Whenever I click on this close button, the f ...

Using TypeScript to pass parameter in a chained function

When using node.js, I am calling a script with `exec` from the `child_process` module. In the closed state, I need to verify if there was any error so that I can handle it properly. However, I am facing an issue where I cannot access the `error` parameter ...

Add the list of information during the looping process (map)

I am currently facing a challenge where I need to update my data during the iteration and send the results to an API call. The API Call expects a request with data structured in the following format: { list: [{ id: "1", name: "Hello" ...

Perform asynchronous tasks to assign values to properties of an object within a forEach loop, and then retrieve the object after all asynchronous tasks have been completed

I'm currently working on a code that involves iterating over an array and performing asynchronous tasks for each element in the array. The objective is to execute async operations, retrieve a value from it and assign that value to an object's pro ...

Using tRPC and React-query for optimistic updates is resulting in frequent and distracting flashing updates

As I implement optimistic updates using the tRPC useMutation React-Query hook, everything seems to be working fine. However, there is a peculiar issue occurring after updating the data - the response quickly changes to the new list but then switches back t ...

Is there a more efficient way to implement class binding in Vue?

Seeking advice regarding a component with dynamic classes that appears cluttered. Struggling to determine if there's a more efficient way to streamline the code, particularly when it comes to vue's class binding feature. Working in pug, although ...

Challenges with namespace and require("uniqid") in Typescript when using @types/uniqid

Upon adding @types/uniqid using the command npm install --save @types/uniqid to utilize in a class, I encountered an issue when trying to instantiate this class with new. If I include import uniqid = require("uniqid"); at the beginning of the page to use ...

Is there a way to make a parameter optional instead of mandatory?

Interactive Editor My goal is to create a function where undefined values are optional, while non-optional values remain required. Here is an example: type TypeObj = { a: undefined; b: number; c: string; } call("a"); call("b", 1); call("c", "3"); / ...

Working with Angular 4: Utilizing HttpResponse in a Component

I am attempting to retrieve the response from my POST request using Angular 4. Below is the code I am using: app.component.html: `findAccordiSmall(pagination: Pagination) { this.accordiListTableLoading = true; debugger; this.ac ...

The reason behind the clickable issue with my duplicate <ion-select>

I've been working on a form using HTML, CSS, and JavaScript where I implemented an "Add" button to duplicate the form. The issue arises with the ion-select element within the duplicated form. While the original form displays all predefined options upo ...

Implement tooltip functionality in ssr chart using echarts

A chart is generated using echarts on the server-side: getChart.ts const chart = echarts.init(null, null, { renderer: 'svg', ssr: true, width: 400, height: 300 }); chart.setOption({ xAxis: { data: timeData }, ...

Ensuring type safety in TypeScript arrow function parameters

I have encountered an issue with my code when setting "noImplicitAny" to true. import ...; @Injectable() export class HeroService { private _cachedHeroes: Observable<Hero[]>; private _init: boolean; private _heroesObserver: Observer<Hero[ ...

What is the best way to utilize the fresh Sanitizer API in Typescript?

Everything seems to be working well on Codepen, even without using window. It's surprising because I'm used to having to use window.x if ( 'Sanitizer' in window ) { console.log( 'sani', 'Sanitizer' in window ); } ...

Error TS2403: All variable declarations following the initial declaration must be of the same type in a React project

While developing my application using Reactjs, I encountered an error upon running it. The error message states: Subsequent variable declarations must have the same type. Variable 'WebGL2RenderingContext' must be of type '{ new (): WebGL2 ...

Async and Await with Typescript

After searching extensively, I couldn't find a similar issue. I am working with Ionic 4 in Angular 7 along with Typescript 3.16. I have multiple 'TimeSpan' values that I need to retrieve using a function from the HTML like so: <ion-input ...

There was an issue with the rendering: "TypeError: Unable to access the property 'text' because it is undefined"

There is an issue when it comes to rendering the content.link.text property in this code. It shows a Error in render: "TypeError: Cannot read property 'text' of undefined", although there are no errors when passing the title and description. Par ...

Issues with React TypeScript type definitions not functioning as expected

When I installed typings, I encountered this error in the terminal: Error Message in Terminal Error TS2320: Interface 'Element' cannot extend types 'ReactElement<any>' and 'ReactElement<any>'. The named property ...