当前位置: 首页 > news >正文

Vue——vue3 之 数据字典管理

背景问题:
需要统一管理系统中的数据字典。

方案思考:
创建数据字典管理模块,统一管理枚举值和选项。

具体实现:
数据字典管理:

// stores/modules/dict.jsimport{defineStore}from'pinia'import{ref}from'vue'import{getDictList}from'@/api/dict'exportconstuseDictStore=defineStore('dict',()=>{constdictMap=ref({})// 获取字典数据constgetDictData=async(dictType)=>{if(dictMap.value[dictType]){returndictMap.value[dictType]}try{constresponse=awaitgetDictList(dictType)dictMap.value[dictType]=response.datareturnresponse.data}catch(error){console.error(`获取字典${dictType}失败:`,error)return[]}}// 获取字典标签constgetDictLabel=(dictType,value)=>{constdictData=dictMap.value[dictType]||[]constitem=dictData.find(item=>item.value===value)returnitem?item.label:value}// 批量获取字典constbatchGetDictData=async(dictTypes)=>{constpromises=dictTypes.map(type=>getDictData(type))constresults=awaitPromise.all(promises)returnresults}// 清除字典缓存constclearDictCache=(dictType)=>{if(dictType){deletedictMap.value[dictType]}else{dictMap.value={}}}return{dictMap,getDictData,getDictLabel,batchGetDictData,clearDictCache}})

字典API:

// api/dict.jsimportrequestfrom'@/utils/request'// 获取字典列表exportfunctiongetDictList(dictType){returnrequest({url:`/system/dict/data/type/${dictType}`,method:'get'})}// 获取所有字典类型exportfunctiongetAllDictTypes(){returnrequest({url:'/system/dict/type/list',method:'get'})}// 获取字典选项exportfunctiongetDictOptions(dictType){returnrequest({url:`/system/dict/data/type/${dictType}`,method:'get'}).then(response=>{returnresponse.data.map(item=>({label:item.dictLabel,value:item.dictValue,disabled:item.status==='1'}))})}

字典组件:

<!-- components/DictSelect.vue --> <template> <el-select v-model="selectedValue" :placeholder="placeholder" :clearable="clearable" :disabled="disabled" @change="handleChange" > <el-option v-for="item in dictOptions" :key="item.value" :label="item.label" :value="item.value" :disabled="item.disabled" /> </el-select> </template> <script setup> import { ref, computed, onMounted, watch } from 'vue' import { useDictStore } from '@/stores/modules/dict' const props = defineProps({ modelValue: [String, Number], dictType: { type: String, required: true }, placeholder: { type: String, default: '请选择' }, clearable: { type: Boolean, default: true }, disabled: { type: Boolean, default: false } }) const emit = defineEmits(['update:modelValue', 'change']) const dictStore = useDictStore() const dictOptions = ref([]) const selectedValue = computed({ get: () => props.modelValue, set: (value) => emit('update:modelValue', value) }) const handleChange = (value) => { emit('change', value) } // 获取字典数据 const loadDictData = async () => { const data = await dictStore.getDictData(props.dictType) dictOptions.value = data.map(item => ({ label: item.dictLabel, value: item.dictValue, disabled: item.status === '1' })) } onMounted(() => { loadDictData() }) // 监听字典类型变化 watch(() => props.dictType, () => { loadDictData() }) </script>
http://www.jsqmd.com/news/320318/

相关文章:

  • 2026年油脂分离器厂家技术解析与行业应用指南
  • Flutter for OpenHarmony 电子合同签署App实战 - 主入口实现
  • 2026不锈钢洁净排水系统:技术特性与行业应用解析
  • 2026食品饮料厂洁净区排水系统设计与应用要点
  • 2026免维护不锈钢洁净地漏厂家推荐与选型指南
  • 2026啤酒花回收厂家推荐:高效环保解决方案指南
  • 2026高精度过滤的品牌有哪些推荐
  • Linux命令-lnstat(快速查找文件和目录)
  • 做牛奶浓缩的公司哪家好?2026年行业实力企业推荐
  • Linux命令-local(在函数内定义局部变量)
  • Vidu Agent1.0正式上线:开启YESVIDU全球创意周
  • 腾讯混元3D 3.1全球上线:8视图重建,支持每日20次免费生成与API
  • 手写一个智能指针:从 unique_ptr 到 shared_ptr 的引用计数原理
  • 2026年1月最新评价高的滴头厂家精选推荐
  • 花钱上了 ERP,为什么还是算不出物料需求?
  • 金鹰美术馆GM艺术社区打造全年龄艺术工坊,让艺术成为日常体验
  • 新人采购第一课:怎么说,供应商才会听你?
  • 2026年最新AI短视频工具选型报告:内容特工队AI的效能评估与首选推荐
  • 2026年宁波橱柜定制:专业厂家联系方法与选择指南
  • 2026年新沂条纹砖工厂五强深度测评:谁在引领品质与创新?
  • 2026年企业残保金优化与残疾人就业安置解决方案全景洞察
  • 2026年学校旗杆供应商选型权威指南
  • 2026年顶尖日用品设计服务商:如何选择与矩成创意实力解析
  • 『NAS』在群晖部署一款太空策略游戏-ogame-vue-ts
  • 2026转向轴承厂家综合评测:技术、服务与选型全解析
  • 2026年开年智能色粉机优质供应商甄选指南
  • 『n8n』代码节点
  • 飞书助力clawdbot成为本土化的AI助手
  • 2026宁波全屋原木定制厂家综合实力解析与选择指南
  • 飞函在金融行业的合规应用:如何满足监管机构的通讯要求