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)
      }
    })
  ]
})

 

posted @ 2020-05-31 19:06  无痕-范特西  阅读(2141)  评论(0编辑  收藏  举报