news 2026/7/4 11:16:40

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/7/3 14:32:52

Vue——vue3 之 代码生成器原理

背景问题&#xff1a; 需要理解代码生成器的实现原理。 方案思考&#xff1a; 实现一个简单的代码生成器。 具体实现&#xff1a; 代码生成器&#xff1a; // utils/code-generator.js // 代码生成器类 export class CodeGenerator {constructor(options {}) {this.options…

作者头像 李华
网站建设 2026/7/3 14:32:09

Git 分支核心指南:原理、合并策略与实战技巧

在现代软件开发中&#xff0c;Git 已经成为版本控制的事实标准。而 Git 的精髓&#xff0c;无疑在于其强大而轻量的分支管理能力。要想真正地、高效地利用 Git 进行团队协作和项目管理&#xff0c;就必须深入理解其分支的底层工作机制。这篇笔记将从 Git 的核心指针HEAD开始&am…

作者头像 李华
网站建设 2026/6/29 0:10:39

腾讯混元3D 3.1全球上线:8视图重建,支持每日20次免费生成与API

腾讯混元宣布Hunyuan 3D 3.1在全球平台正式上线&#xff0c;带来纹理保真与几何精度的显著跃升。新版本支持最多8视图输入&#xff0c;大幅提升重建准确度并还原雕刻级细节。为推动规模化应用&#xff0c;新创作者每日可在平台免费生成20次&#xff0c;并同步开放API接口。核心…

作者头像 李华
网站建设 2026/7/3 14:31:49

【开题答辩全过程】以 基于Spring Boot的化妆品销售系统的设计与实现为例,包含答辩的问题和答案

个人简介一名14年经验的资深毕设内行人&#xff0c;语言擅长Java、php、微信小程序、Python、Golang、安卓Android等开发项目包括大数据、深度学习、网站、小程序、安卓、算法。平常会做一些项目定制化开发、代码讲解、答辩教学、文档编写、也懂一些降重方面的技巧。感谢大家的…

作者头像 李华
网站建设 2026/7/3 14:32:20

SPI通信协议详解:从基础概念到硬件配置与编程实现

一、SPI概念SPI&#xff08;Serial Peripheral Interface&#xff0c;串行外设接口&#xff09;是一种同步、全双工、高速的串行通信总线协议&#xff0c;广泛用于微控制器&#xff08;MCU&#xff09;与各种外围设备&#xff08;如传感器、存储器、显示屏、ADC/DAC 等&#xf…

作者头像 李华