nuxt-community / universal-storage-module

Universal Storage Utilities for Nuxt 2
MIT License
276 stars 15 forks source link

Example usage? #55

Closed docmattman closed 3 years ago

docmattman commented 4 years ago

Are there any docs or example usages? I have a couple of store modules, it'd be nice to use this to store persistent data. I'm not sure how to implement the "initialState" though, since the state doesn't have this.$storage available. Trying to do something like this in my store:

export const state = () => ({
  project: this.$storage.syncUniversal('project', {})
})

Am I going about this the wrong way? Should I not try to use the universal storage inside my store? Should I just use this.$storage.syncUniversal('project', {}) inside of a component instead and not even have project in my store? Just looking for a little guidance on basic usage.

printscreen commented 4 years ago

@docmattman Did you ever find a solution to this? I am also curious about how to do this.

farzadso commented 4 years ago

@docmattman Hi,

I don't know why I never saw this issue and going through my emails, I noticed I hadn't received any (which is very strange)

Anyhow, in my personal opinion, if you're trying to do it for a certain route, I recommend using the fetch block to keep the store in sync with the persisted data the user has stored.

If you want to do it for your entire Nuxt.js application, I would move this logic to my nuxtServerInit block and dispatch a function/functions that update your store accordingly. You can read more about nuxtServerInit here : https://nuxtjs.org/guide/vuex-store/#the-nuxtserverinit-action

I hope this answers your question.