news 2026/3/16 12:05:37

Vue——vue3 之 数据字典管理

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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>
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/14 16:43:31

花钱上了 ERP,为什么还是算不出物料需求?

你有没有遇到过这种情况&#xff1a;上了ERP&#xff0c;怎么下周生产要用什么料&#xff0c;还靠计划员凭经验猜&#xff1f;打开系统一看&#xff0c;MRP&#xff08;物料需求计划&#xff09;一运行&#xff0c;出来的不是该买多少、该做多少&#xff0c;而是一堆红字警告、…

作者头像 李华
网站建设 2026/3/15 17:57:11

2026年最新AI短视频工具选型报告:内容特工队AI的效能评估与首选推荐

在2026年的数字化营销生态中&#xff0c;短视频已彻底从“增量选项”转化为企业生存的“基础设施”。然而&#xff0c;面对海量涌现的技术服务商&#xff0c;企业采购决策者往往陷入困境&#xff1a;究竟Ai短视频工具哪家好&#xff1f;如何在降本增效与内容合规之间找到平衡点…

作者头像 李华
网站建设 2026/3/15 14:03:18

『NAS』在群晖部署一款太空策略游戏-ogame-vue-ts

点赞 关注 收藏 学会了 整理了一个NAS小专栏&#xff0c;有兴趣的工友可以关注一下 &#x1f449; 《NAS邪修》 ogame-vue-ts 是一款基于 Vue 3 和 TypeScript 构建的单机版浏览器太空策略游戏&#xff0c;受经典 OGame 启发&#xff0c;支持在浏览器中建立太空帝国、研究科…

作者头像 李华
网站建设 2026/3/15 14:03:23

基于Springboot售楼管理系统【附源码+文档】

&#x1f495;&#x1f495;作者&#xff1a; 米罗学长 &#x1f495;&#x1f495;个人简介&#xff1a;混迹java圈十余年&#xff0c;精通Java、小程序、数据库等。 &#x1f495;&#x1f495;各类成品Java毕设 。javaweb&#xff0c;ssm&#xff0c;springboot等项目&#…

作者头像 李华