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

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

需求

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

方案一

在这里插入图片描述

需要做到的要求

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

综上

utils/dictionary.js

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 = noTitle
      listData.some(item => {
        if (item.value === value) {
          label = item.label
          return true
        }
      })
      return label
    }
  } catch (err) {
    console.error(err)
    return (value) => noTitle
  }
}

下拉框组件

created () {
    this.getOptionLists()
  },

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

table组件中

<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



// 预加载字典
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就行,根本不用上面那些,先都记录上,后期根据需求灵活应用吧

相关文章:

  • C++ Primer Plus第八章课后习题总结
  • win11系统通过WSL安装ubuntu
  • 模电学习笔记
  • 服务端配置TCP探活,超出探活时间后的行为?
  • 7.grafana的内存和CPU同时在一个表中的调整
  • 汽车悬架系统技术演进:从被动到全主动的革新之路(主动悬架类型对比)
  • 【单片机】MSP430MSP432入门
  • 大白话Vuex 核心概念(state、mutations、actions)的使用案例与原理
  • vue项目中动态添加类名样式不生效问题
  • 2025-02-26 学习记录--C/C++-C语言 整数格式说明符
  • 简单介绍JVM
  • Flutter系列教程之(6)——Tab导航与ListView使用
  • Lua的table类型的增删改查操作
  • http 协议和 https 协议的区别是什么?
  • 【1分钟学会万相文生视频】windows环境4080显卡部署Wan2.1-T2V-1.3B,亲手实操
  • 【STL】7.STL常用算法(1)
  • miqiu的分布式锁(二):实战——用JMeter验证JVM锁能否解决MySQL超卖问题
  • element-ui的组件使用
  • 每日一题——字母异位词分组
  • 2024 年出现的 11 大数据收集趋势
  • 网站分类目录大多数依靠的是人为提交而不是蜘蛛/页面优化
  • 我英文网站建设/网站怎么优化排名靠前
  • PHP网站开发项目式教程/sem优化策略
  • 建设网站用英文怎么说/域名交易中心
  • 如何用dw8做网站视频/百度seo优化策略
  • 做内贸的有哪些网站/网上国网app推广