[solved] Why is vuex defined as a function in nuxt?

Business background

  • Learning nuxt static vue Project
  • migrate a completed vue project to the nuxt framework

Perplexities encountered

  • in vue , the definition vuex is an instance object defined directly

the code is as follows:

export default new Vuex.Store({
  state: {
  },
  mutations: {
  },
  debug: true
});
  • but in the nuxt official document, it is defined in this way

sample code is as follows:

const createStore = () => {
  return new Vuex.Store({
    state: {
    },
    mutations: {
    }
  })
}

question

  • nuxt what is the reason for doing this? Instantiate objects only when called to save performance?
  • if I want to call the store object on another non-component page, don"t I have to re-instantiate it again?
Jul.14,2021

when your vuex needs some Synchronize rendered data, the function can accept context objects

Menu