javascriptvue.jscookiesnuxt.jsjs-cookie

I build a app in Vue. I need to store information for later use, is it a good way to use js-cookie?


I have tried to use Vuex for storing information that the user choose in my app, but as soon i reload the page its gone. Then i started to use Cookies, js-cookie and its works perfect. I feel like that has to be something bad with it, why would you use vuex if you just could use a cookie? I have not found any problems with using cookies but it feels at someway that is has to be something bad about it.

which way is best to go when you need to gather information and without it disappearing when you reload the page?


Solution

  • Vuex is an in-memory storage. That gets cleared up as soon as the page reloads. You want a more persisted storage. There are several options, localstorage, sessionstorage, cookie and indexedDB.

    Cookies are sent along with every request you make to the server. That is probably not necessary for you or what you want.

    Localstorage should do just fine.

    Check out vuex-persistedstate that synchronizes between Vuex and localstorage.

    https://github.com/robinvdvleuten/vuex-persistedstate

    That's probably what you want to use.