Persistent Vuex Store For Only Specific Modules
--
Here we go again š , another yet exciting and educating article. Good day people, Iām so glad youāre reading this article. Today, weāre going to talk about how to create a persistent state for our vuex store.
Iām sure most of you already know how to make a vuex state persistent, but hereās the fun part, we can choose which module to make persistent. This way, we use less storage on our userās PC.
Okay, letās dive in.
First off, Iām sure weāve already set up our vuejs project and vuex state, so Iām just going to skip that.
so letās start, we first need to install the package that helps us make state persistent. so letās run;
npm i vuex-persistedstate
done? After that, we move to set it up. Letās go into our store directory and then into the index.js file.
After that, we need to import this package. Go ahead and import it.
After importing it, we now import our modules.
Thatās great. Weāve now imported all the modules we created.
Note: You should understand vuejs, vuex store and state management before reading this article.
Letās continue.
In our current use case, we have 5 modules in our state but not all of them are required to be persistent. The necessary modules that should be persistent include: cart, and account.
Now, we are saving the cart module because we need the cart to always be in the state, basically to be saved on your userās device, and the account because of the user information.
Now to make these modules persistent, we write the following code in our index.js.
and thatās pretty much each. As you can see, we set paths that our createPersistedState() function should save in state.
Think we didnāt save all the necessary modules, go ahead and write the modules you need to be saved.
Each module should be separated with a comma(,).
And thatās it, Itās a wrap.
Thanks again for reading another of my article. Also, if you think this article deserves your claps, go ahead and clap, Iād appreciate every clap.
Much love from bolathedev ā¤ļø!