vuex持久化 vuex-persistedstate

1.下载

npm install --save vuex-persistedstate

2.在vuex里面引入,默认是localStorage

import persistedState from ‘vuex-persistedstate‘
export default new Vuex.Store({
    // ...
    plugins: [persistedState()]
})

3.使用sessionStorage

plugins: [
    persistedState({ storage: window.sessionStorage })
]

4.使用cookie的情况

import persistedState from ‘vuex-persistedstate‘
import * as Cookies from ‘js-cookie‘

export default new Vuex.Store({
  // ...
  plugins: [
    persistedState({
      storage: {
        getItem: key => Cookies.get(key),
        setItem: (key, value) => Cookies.set(key, value, { expires: 7 }),
        removeItem: key => Cookies.remove(key)
      }
    })
  ]
})

相关推荐