rubystarashe / nuxt-vuex-localstorage

MIT License
161 stars 18 forks source link

How to use localstorage for 'index' module (default) #8

Closed pun-ky closed 5 years ago

pun-ky commented 5 years ago

In few places in my app I am using default store (not namespaced) and I also want to have it synced with local storage.

image

Is it possible to set somehow localstorage working also for store/index.js values without moving it into named module (non-index) ?

While using 'index' it is not working... :/

rubystarashe commented 5 years ago

Check this.

//  nuxt.config.js
module.exports = {
  modules: [
    ['nuxt-vuex-localstorage', {
      localStorage: ['foo', 'bar'],  //  If not entered, “localStorage” is the default value
      sessionStorage: ['sfoo', 'sbar']  //  If not entered, “sessionStorage” is the default value
    }]
  ]
}

// store/index.js
export const state = () => ({
  foo: {
    anyValues: 0
  },
  bar: {
    anyValues: 0
  },
  sfoo: {
    anyValues: 0
  },
  sbar: {
    anyValues: 0
  }
})

Use ['foo', 'bar', 'tumbleRabbit', 'sweet', 'dino'] instead of ['index', 'tumbleRabbit', 'sweet', 'dino']

This module would observe all items of store with deep options. store/tumbleRabbit.js in nuxt store module usage is the same as this of store/index.js

// store/index.js
export const state = () => ({
  tumbleRabbit: value
})
pun-ky commented 5 years ago

Fine thank you