• [vuex]字典值封装到vuex缓存


    store/modules/cache.js  

    import { listData } from '@/api/system/dict/data.js'
    
    const state = {
        dictCache: {},
    }
    const mutations = {
        UPDATE_DICTS(state, payload) {
            state.dictCache = payload
        }
    }
    const actions = {
        getDictCache({ commit }) {
            listData().then((res => {
                if (res.code == 200) {
                    console.log(res.rows);
                    let tempCache = {}
                    res.rows.forEach(item => {
                        if (tempCache[item.dictType]) {
                            tempCache[item.dictType].push(item)
                        } else {
                            tempCache[item.dictType] = [{...item }]
                        }
                    })
                    commit('UPDATE_DICTS', tempCache)
                }
            }))
        }
    }
    export default {
        state,
        mutations,
        actions,
        namespaced: true
    }

    store/modules/index.js  

    import Vue from 'vue'
    import Vuex from 'vuex'
    import getters from './getters'
    import cache from './modules/cache'
    
    Vue.use(Vuex)
    
    const store = new Vuex.Store({
        modules: {
            cache
        },
        getters
    })
    
    export default store

    store/modules/getters.js  

    const getters = {
        cache: state => state.cache,
    }
    export default getters

    App.vue

    created() {
      this.$store.dispatch("cache/getDictCache");
    },

    使用方法

        computed: {
            ...mapGetters(["cache"]),
        },
        methods:{
           console.log(this.cache)
        }    
  • 相关阅读:
    Go 打印出结构化结构体
    GOPROXY设置
    python判断链表是否有环
    单链表python和go的代码
    mongo索引
    python修改srt字幕的时间轴
    python各个版本的排序
    mac使用python识别图形验证码
    selenium运行js代码笔记
    布隆过滤器
  • 原文地址:https://www.cnblogs.com/lv77/p/14861159.html
Copyright © 2020-2023  润新知