• vuex-persist数据持久化存储插件


    Vuex 解决了多视图之间的数据共享问题。但是运用过程中又带来了一个新的问题是,Vuex 的状态存储并不能持久化。也就是说当你存储在 Vuex 中的 store 里的数据,只要一刷新页面,数据就丢失了。

    引入vuex-persist 插件,它就是为 Vuex 持久化存储而生的一个插件。不需要你手动存取 storage ,而是直接将状态保存至 cookie 或者 localStorage 中。具体用法如下

    pexels-photo-1211847.jpeg

    安装:

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

    引入:

    import VuexPersistence from 'vuex-persist'
    

    先创建一个对象并进行配置:

    const vuexLocal = new VuexPersistence({
        storage: window.localStorage
    })
    

    引入进vuex插件:

    const store = new Vuex.Store({
      state: { ... },
      mutations: { ... },
      actions: { ... },
      plugins: [vuexLocal.plugin]
    }) 
    

    通过以上设置,在图3中各个页面之间跳转,如果刷新某个视图,数据并不会丢失,依然存在,并且不需要在每个 mutations 中手动存取 storage 。

    vuex-persist 的详细属性:

    属性类型描述
    key string 将状态存储在存储中的键。默认: 'vuex'
    storage Storage (Web API) 可传localStorage, sessionStorage, localforage 或者你自定义的存储对象. 接口必须要有get和set. 默认是: window.localStorage
    saveState function (key, state[, storage]) 如果不使用存储,这个自定义函数将保存状态保存为持久性。
    restoreState function (key[, storage]) => state 如果不使用存储,这个自定义函数处理从存储中检索状态
    reducer function (state) => object 将状态减少到只需要保存的值。默认情况下,保存整个状态。
    filter function (mutation) => boolean 突变筛选。看mutation.type并返回true,只有那些你想坚持写被触发。所有突变的默认返回值为true。
    modules string[] 要持久化的模块列表。

    原文

  • 相关阅读:
    使用snmp+mrtg监控CPU、流量、磁盘空间、内存
    ISO20000
    nginx入门篇----nginx服务器基础配置
    oracle数据库备份和还原
    oracle创建删除用户和表空间
    Centos 6.5安装oracle 11g
    nginx入门篇----安装、部署、升级
    vue 高德地图 地图初始化显示接口返回的多个经纬度
    vue element UI el-table 单元格中超出字省略号显示
    vue + element ui 打印
  • 原文地址:https://www.cnblogs.com/wang-sai-sai/p/11076833.html
Copyright © 2020-2023  润新知