[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)
    }    

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM