• Vue状态管理 Storage Vuex Pinia


    趁着周末编写Vue有关的状态管理插件 , 抛出三个方法

    { 
        Storage,
        createVuexPersistedState,
        createPiniaPersistedState 
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Storage 直接操作H5缓存特性 【window.sessionStorage,window.localStorage】

    let s = new Storage({source:window.localStorage}) //默认为window.localStorage
    
    s.get(key)
    
    s.set(key,value,expires=undefined) // expires 缓存的时间 单位为毫秒 
    
    • 1
    • 2
    • 3
    • 4
    • 5

    createVuexPersistedState 配合Vuex 进行状态管理

    createVuexPersistedState({
    
        key:'vuex', // 默认key为'vuex'
    
        storage:window.localStorage,//默认为 window.localStorage
    
        whiteList:[], // 白名单 配置缓存的key  通常配置whiteList
    
        blackList: [], // 黑名单 配置不缓存的key
    
    }) 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    createPiniaPersistedState 配合新版状态管理 Pinia

    store.js

    store.use(createPiniaPersistedState()); 
    
    //or 
    
    store.use(createPiniaPersistedState({ 
        key:'You want the cache key', // 默认为‘pinia-’ + 你的模块ID
        storage:'The way you want to cache' // 缓存方式window.sessionStorage || window.localStorage  默认window.localStorage
    })); 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    模块js

    import { defineStore } from "pinia"; 
    const useUser = defineStore("user", { 
        state: () => { 
            return { userName: "zhangsan", 
        }; },
        getters: {}, 
        actions: {}, 
        whiteList: ["userName"], // 白名单 需要缓存的key  默认为全部参与缓存 
    }); 
    export default useUser; 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    以下为三个方法具体使用示例

    explain

    This plug-in is useful for state management because it is impossible to predict how a user will cache, exposing three methods: Storage createVuexPersistedState createPiniaPersistedState

    Installation

    Package Manager

    # npm
    npm i vue-persistedstate
    
    # yarn
    yarn add vue-persistedstate 
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Storage example

    /**
     * @name: s
     * @param {key:string}
     * @return {value:any}
     * @return {expires:number}
     */
    import { Storage } from "vue-persistedstate"
    
    let s = new Storage({source:window.sessionStorage}) //The default value is the window. The localStorage
    s.set("userInfo",{uName:"zhangsan",upwd:"123456"},5000)
    setInterval(()=>{
    console.log(s.get('uName'))  // Undefined is displayed after 5s
    },1000) 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    Vuex example

    The createVuexPersistedState method has four parameters

    1. Key The default cache key is VUex
    2. Storage how caching (window. SessionStorage | | window. The localStorage) default window. The localStorage
    3. WhiteList Attributes are cached
    4. BlackList Indicates that blackList attributes are not cached
    import { createVuexPersistedState } from "vue-persistedstate";
    
    /**
     * @name: createVuexPersistedState
     * @param {key:string}
     * @param {storage}
     * @param {whiteList:Array<string>}
     * @param {blackList:Array<string>}
     * @return {storage}
     */
    
    export default new Vuex.Store({
      plugins: [
        createVuexPersistedState({
          key:'vuex',
          storage:window.localStorage,
          whiteList:[],
          blackList: [],
        }),
      ],
      modules: {},
      state: {},
      mutations: {},
      actions: {},
      modules: {},
    }); 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    Pinia example

    store.js

    import { createPinia } from "pinia";
    
    import { createPiniaPersistedState } from "vue-persistedstate";
    
    const store = createPinia();
    
    store.use(createPiniaPersistedState());
    //or
    store.use(createPiniaPersistedState({
        key:'You want the cache key',  // Default is pinia- your Module ID
        storage:'The way you want to cache' // The default value is the window. The localStorage
    }));
    
    export default store; 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    modules->moduleJS example

    Set whiteList in the module: Array, whiteList is empty or not set to all cache, default is all cache, whiteList can be set to cache other keys do not cache

    import { defineStore } from "pinia";
    const useUser = defineStore("user", {
      state: () => {
        return {
          userName: "zhangsan",
        };
      },
      getters: {},
      actions: {},
      whiteList: ["userName"], // Only userName is cached
    });
    
    export default useUser; 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    maven学习笔记——感谢尚硅谷官方文档
    【luogu P4887】【模板】莫队二次离线(第十四分块(前体))(莫队)
    es6_字符串扩展
    随smart登陆欧洲,亿咖通科技踏上出海新征程
    Spring之bean的生命周期
    图像处理与计算机视觉--第七章-神经网络-单层感知器
    【网络安全带你练爬虫-100练】第20练:数据处理-并写入到指定文档位置
    flask+Pyecharts+ajax实现分tab页展示多图
    静态链接库(Lib) 与 动态链接库(DLL)
    UE5笔记【三】UE5材质Materials
  • 原文地址:https://blog.csdn.net/web2022050903/article/details/125520731