0
votes

I have a vue app built in Nuxt which has vue routing / history enabled.

The problem is when I save something to the app state / Vuex, it changes on the single page fine but when I navigate to a new page it's been cleared and returns the state to it's default values.

Do I have to do something to enable the state management in vuex to work on different pages?

store/index.js

import Vuex from "vuex"

const createStore = () => {
  return new Vuex.Store({
    state: {
      count: 10,
      username: "John Smith",
      loggedIn: false
    },
    mutations: {
      loginStatus(state, status) {
        state.loggedIn = status
      },
      setUsername(state, name) {
        state.username = name
      }
    }
  })
}

export default createStore

Code from the vue app

 this.$store.commit("loginStatus", true)
 this.$store.commit("setUsername", "Adam")
1
Do u use nuxt-link/router-link to navigate ? - Aldarund
yes, the nuxt-link is used throughout - flo
than it shouldt happen. vuex persistedstate needed only for full page reloads - Aldarund

1 Answers

0
votes

I've added https://www.npmjs.com/package/vuex-persistedstate to the project and it seems to have fixed the problem but let me know if there is an easier way.