In order to save the data inside of Vuex, you need to have an initial state that is loaded with the application and defined as the default one when the user enters your application. Here, we are going to learn how to create the Vuex state and use it as a singleton, so that Vuex can be used in an SPA and an SSR page:
Now we will create a Vuex store that can be used in an SSR and an SPA:
- In the src/store folder, create a new folder called user, and inside this folder create a new file named state.js.
- Create a new generateState function. This function will return a JavaScript object, with three main properties, data, loading, and error. The data property will be a JavaScript object, with a property called usersList defined as an empty array as default, and a property called userData with the default object of a user. The loading property will be a boolean, set to false by default, and error will have a default value initializing to null:
const generateState = (...