Persistent Vuex Store For Only Specific Modules

bolathedev
2 min readMay 12, 2022

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 you already know this

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 ā¤ļø!

--

--