通过返回的key值匹配字典中的value值

news/2025/3/3 6:40:33/

需求

页面中上面搜索项有获取字典枚举接口,table表格中也有根据key匹配字典中的value

方案一

在这里插入图片描述

需要做到的要求

  • 这里上面下拉列表是一个组件获取的字典,下面也是通过字典匹配,所以尽量统一封装一个函数,每个组件保证最少变动
  • table表格中如果filters中更改,会获取多次接口,—使用闭包让只获取一次接口,节省性能
  • filter中又是必须同步函数,异步函数报错,所以不能使用async,await
  • 最好请求接口之后存储起来下次直接拿不用请求了

综上

utils/dictionary.js

javascript">import { getDicValue } from '@/api/systemManage'
import store from '@/store'
/*** 获取字典数据* @param {Boolean} hasAllOption 是否包含全部选项*/
export function getDicValueList(dictClassCode, hasAllOption = true) {const dictionaryCache = store.state.dictionary.dictionaryCache || {}return new Promise((resolve, reject) => {let optionList = []if (dictionaryCache[dictClassCode]) {const dicList = JSON.parse(JSON.stringify(dictionaryCache[dictClassCode]))if (hasAllOption) {optionList = [ { value: '', label: '全部' }, ...dicList]} else {optionList = [...dicList]}resolve(optionList)} else {getDicValue(dictClassCode).then(response => {console.log('字典调用', dictClassCode)const dicList = response.value || []store.dispatch('dictionary/setDictionaryCache', { key: dictClassCode, value: dicList })if (hasAllOption) {optionList = [ { value: '', label: '全部' }, ...dicList]} else {optionList = [...dicList]}resolve(optionList)}).catch(error => {reject([])})}})
}/*** 获取字典数据并返回一个闭包函数* @param {string} dictClassCode 字典类代码* @param {string} noTitle 默认值,当找不到匹配项时返回* @returns {Promise<Function>} 返回一个闭包函数,该函数可以根据 value 获取 label*/
export async function getDicValueName(dictClassCode, noTitle = "--") {try {const response = await getDicValueList(dictClassCode, false)const listData = response || []return (value) => {let label = noTitlelistData.some(item => {if (item.value === value) {label = item.labelreturn true}})return label}} catch (err) {console.error(err)return (value) => noTitle}
}

下拉框组件

javascript">created () {this.getOptionLists()},getOptionLists() {if (this.dictClassCode) {getDicValueList(this.dictClassCode).then(res => {this.optionLists = res})} else {this.optionLists = this.options}},

table组件中

javascript"><span>{{ filterStateName(scope.row.state) }}</span>import { getDicValueName } from '@/utils/dictionary'created() {this.initDictionary()},methods: {
async initDictionary() {try {this.filterStateName = await getDicValueName('DC_DICTIONARY_STATE')} catch (error) {console.error('Failed to fetch dictionary:', error)this.filterStateName = (value) => '--'}},}

问题: 但是这种因为一进页面这两个组件都是去获取字典,所以还是从接口拿的数据,会调用两次接口

方案二(建议)

方案:接下来优化可以通过路由导航预加载,先获取字典之后,在加载页面
router.js

javascript">// 预加载字典
export function preloadDictionary(dictClassCodeList) {const pList = []dictClassCodeList.forEach(dictClassCode => {const p = getDicValueList(dictClassCode)pList.push(p)})return Promise.all(pList)
}// 预加载字典
router.beforeEach((to, from, next) => {if(to.meta && to.meta.dictClassCodeList&& to.meta.dictClassCodeList.length > 0) {preloadDictionary(to.meta.dictClassCodeList).then(res => {next()})} else {next()}
})

总结:其实这里都可以规定直接预加载字典,到页面直接使用加载后的字典,注册个全局filters就行,根本不用上面那些,先都记录上,后期根据需求灵活应用吧


http://www.ppmy.cn/news/1576225.html

相关文章

苹果CMS泛目录优化全攻略:局部URL随机化与数据无缝对接

引言 在当今的SEO优化领域&#xff0c;泛目录和站群策略已经成为提升网站权重和流量的重要手段。苹果CMS作为一款功能强大的内容管理系统&#xff0c;其新版框架为开发者提供了丰富的二次开发接口&#xff0c;使得泛目录和站群策略的实现变得更加灵活和高效aoshunseo。本文将详…

基于STM32的智能家居能源管理系统

1. 引言 传统家庭能源管理存在能耗监控粗放、设备联动不足等问题&#xff0c;难以适应绿色低碳发展需求。本文设计了一款基于STM32的智能家居能源管理系统&#xff0c;通过多源能耗监测、负荷预测与优化调度技术&#xff0c;实现家庭能源的精细化管理与智能优化&#xff0c;提…

SpringBoot3—核心特性:基础特性

一、SpringApplication &#xff08;1&#xff09;自定义 banner 类路径添加 banner.txt 或设置 spring.banner.location 就可以定制 banner推荐网站&#xff1a;Spring Boot banner在线生成工具&#xff0c;制作下载英文banner.txt&#xff0c;修改替换banner.txt文字实现自…

无服务边缘融合架构:重新定义云原生应用边界

引言&#xff1a;零部署计算的革命突破 Airbnb迁移至LambdaEdge架构后&#xff0c;全球客房详情页渲染延迟降至35ms&#xff0c;冷启动时间缩至50ms以内。Stripe采用无服务边缘计算处理支付事务&#xff0c;成功将动态API响应P99延迟从210ms压缩至19ms。AWS官方基准显示&#…

HTTP/1.0、HTTP/1.1、HTTP/2 核心区别对比

前言 经常开发的小伙伴估计对http都不陌生&#xff0c;下面来看看的之间的区别是啥&#xff1f; 一、连接管理 ‌HTTP/1.0‌ 每个请求需单独建立和关闭 TCP 连接&#xff0c;无法复用&#xff0c;导致高延迟和资源浪费‌。 无状态设计&#xff0c;服务器不记录客户端上下文…

数据集笔记:新加坡LTA Taxi Availability

1 数据集介绍 获取新加坡可用出租车的位置每30秒从LTA的Datamall获取一次数据 建议每分钟调用此端点 响应是一个有效的GeoJSON&#xff0c;可以将其插入到如Leaflet这样的地图工具中由于LTA不提供任何元数据&#xff0c;因此响应中的时间戳为抓取数据的时间使用date_time参数以…

告别GitHub连不上!一分钟快速访问方案

一、当GitHub抽风时&#xff0c;你是否也这样崩溃过&#xff1f; &#x1f621; npm install卡在node-sass半小时不动&#x1f62d; git clone到90%突然fatal: early EOF&#x1f92c; 改了半天hosts文件&#xff0c;第二天又失效了... 根本原因&#xff1a;传统代理需要复杂…

Flutter 学习之旅 之 flutter 在 Android 端进行简单的图片裁剪操作

Flutter 学习之旅 之 flutter 在 Android 端进行简单的图片裁剪操作 目录 Flutter 学习之旅 之 flutter 在 Android 端进行简单的图片裁剪操作 一、简单介绍 二、简单介绍 image_cropper 三、安装 image_picker 四、简单案例实现 五、关键代码 一、简单介绍 Flutter 是一…