首页 > 其他 > 详细

若依字典封装到vuex缓存

时间:2021-06-08 10:00:55      阅读:82      评论:0      收藏:0      [点我收藏+]

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

 

若依字典封装到vuex缓存

原文:https://www.cnblogs.com/lv77/p/14861159.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!