news 2026/5/1 5:54:06

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/4/30 8:43:57

如何将某个成员设置为管理员?看这里!

&#x1f64b;能否将某个相册成员设置为管理员&#xff0c;协助我管理相册&#xff1f;&#x1f449;支持的⬇️下面将介绍如何将某个成员设置为管理员&#xff1a;1️⃣打开土著相册小&#x1f34a;序&#xff0c;点击目标相册&#xff0c;进入相册2️⃣点击底部按钮「管理」&…

作者头像 李华
网站建设 2026/4/18 15:12:40

Linux命令-lnstat(快速查找文件和目录)

&#x1f9ed;说明 locate 命令是 Linux 中一个用于快速查找文件和目录的工具&#xff0c;它通过搜索系统预先生成的文件名数据库来工作&#xff0c;速度非常快。下面我将详细介绍它的用法、与 find 命令的区别以及一些实用技巧。 &#x1f50d; locate 与 find 的区别 在深…

作者头像 李华
网站建设 2026/5/1 4:50:16

飞书助力clawdbot成为本土化的AI助手

飞书助力clawdbot成为本土化的AI助手 先看视频 废话少说&#xff0c;先看视频&#xff1a; clawdbot 简介 视频是用飞书pc端模拟手机端&#xff0c;来控制clawdbot的。 clawdbot已经火到了macmini涨价的地步&#xff0c;为什么我们用起来那么难&#xff1f; 看看主页说明中…

作者头像 李华
网站建设 2026/4/28 4:17:54

一个相当复杂的跨品牌电梯智能群控系统项目,涉及硬件改造、软件调试和系统集成。从多奥提供的详细清单和流程来看,用户很可能是电梯智能化改造项目的技术负责人或系统集成商,需要确保整个方案从准备到验收的顺利

现在需要我帮助梳理和优化这个技术方案&#xff0c;使其更具可操作性和系统性。我打算从项目全生命周期的角度&#xff0c;构建一个逻辑清晰、阶段分明的实施框架。 首先考虑的是项目前期准备阶段&#xff0c;这是整个项目的基础。根据我看到的搜索结果&#xff0c;现场勘察与…

作者头像 李华
网站建设 2026/4/23 13:43:38

Dropbear SSH Server - 工程级 Bug 修复方案

一、Critical 级别修复(立即修复) BUG #1: circbuffer.c 空指针解引用 问题位置: circbuffer.c:93-106 原始代码: void cbuf_readptrs(const circbuffer *cbuf,unsigned char **p1, unsigned int *len1, unsigned char **p2, unsigned int *len2) {*p1 = &cbuf->…

作者头像 李华
网站建设 2026/4/28 12:48:52

2026美国大学生数学建模竞赛时间安排

2026美赛数学建模A题B题C题D题E题F题思路模型代码论文持续更新&#xff0c;完整论文见文末名片2026年MCM/ICM美赛已进入冲刺倒计时&#xff0c;各位参赛小伙伴想必都已组队完毕&#xff0c;摩拳擦掌准备迎战这96小时的脑力攻坚战&#xff0c;向着好成绩全力奔赴&#xff01;美赛…

作者头像 李华