I recently added persisted state to my Vue application using the command npm install --save vuex-persistedstate.
After that, I configured my Vuex store.js file in the following way:
import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)
export const store = new Vuex.Store({
state: {
title: ''
},
mutations: {},
actions: {},
getters: {},
plugins: [createPersistedState()]
})
..In another component, I set the value for store.title like this:
this.$store.state.title = this.input
This approach has been effective so far.
However, when I refresh the page (using F5), the stored value from this.input
no longer persists in this.$store.state.title
, causing a malfunction in my website.
What could I be doing incorrectly?
UPDATE: I found an alternative solution without relying on the Vuex plugin:
First:
this.$store.state.title = this.input
localStorage.setItem('title', this.$store.state.title)
Then:
mounted () {
this.$store.state.title = localStorage.getItem('title')
}
I'm still curious about why the Vuex plugin is not functioning as expected :(