简单使用 Vuex - Persistence

106 阅读1分钟

vuex-persistedstate:可以自动保存 vuex 中的数据到 localStorage,并且在下次开始的时候自动读取这个数据到对应的 state 中

下载依赖

npm install --save vuex-persist

使用方法

store文件内引入

必须注册 plugins


import craetePersistedstate from 'vuex-persistedstate'

const store = createStore({
  ......
  plugins: [
    craetePersistedstate({
      // 指定保存到 localStorage中的key
      key: 'imooc-front',
      // 需要保存的模块
      paths: ['category']
    })
  ]
})

export default store

效果

11.png