Learn how to automatically store Vuex data to localStorage or sessionStorage
When talking about storing data, there are multiple ways to retain data.
One is very simple, very suitable for prototypes, and usesWeb Storage API: LocalStorage and sessionStorage.
With Vue, you can use the API in a variety of ways. One of the easiest ways is to rely onvuex-persist
library.
You install it using npm or Yarn:
npm install vuex-persist
#or
yarn add vuex-persist
Open the Vuex storage file and add:
import VuexPersist from 'vuex-persist'
Initialize VuexPersist:
const vuexPersist = new VuexPersist({
key: 'my-app',
storage: window.localStorage
})
key
Is the key used in the localStorage database.
changelocalStorage
withsessionStorage
To use other storage systems (each storage system has its own characteristics, please refer to the Web Storage API document I linked above).
Next, addvuexPersist
When initializing the store, go to the list of Vuex plugins:
const store = new Vuex.Store({
//...initialization
plugins: [vuexPersist.plugin]
})
That's it! Every time you change the store, the library will persist it to the browser.
You canOfficial documents, But these are the basis to get you started.
Download mine for freeVue manual
More vue tutorials:
- Vue.js overview
- Vue.js CLI: Learn how to use it
- Vue.js development tools
- Configure VS code for Vue development
- Create your first application with Vue.js
- Vue.js single file component
- Vue.js templates and interpolation
- Vue.js instructions
- Vue.js method
- Vue.js calculated attributes
- Use CSS to style Vue.js components
- Vue.js observer
- Vue method vs observer vs calculated property
- Vue.js filter
- Vue.js components
- Vue.js slot
- Vue.js component props
- Vue.js activities
- Vue.js component communication
- Vuex, Vue.js state manager
- Vue, use a component inside another component
- Vue, how to use prop as a class name
- How to use SCSS with Vue.js single file component
- Use Tailwind in Vue.js
- Vue router
- Dynamically display Vue components
- Vue.js cheat sheet
- Use Vuex to store Vue data to localStorage
- How to use Vue dynamic application class
- Vue, how to use the v model
- Vue, why data must be functions
- Roadmap to become a Vue.js developer in 2020