博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

vuex-persist,解决vuex中的数据刷新页面之后丢失的问题

Posted on 2020-11-10 14:58  Hhuizi  阅读(311)  评论(0编辑  收藏  举报

它是为 Vuex 持久化存储而生的一个插件。不需要你手动存取 storage ,而是直接将状态保存至 cookie 或者 localStorage 中。

使用方法:

  安装:

npm install --save vuex-persist
or
yarn add vuex-persist

  使用:

import VuexPersistence from 'vuex-persist'
// 先创建一个对象并进行配置
const vuexLocal = new VuexPersistence({
    storage: window.localStorage,
    modules: ['users',...] // 需要储存的modules 
})
// 引入vuex插件
const store = new Vuex.Store({
  state: { ... },
  mutations: { ... },
  actions: { ... },
  plugins: [vuexLocal.plugin]
}) 

 详细属性: