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

纷析云开源版- Vue2-增加字典存储到localStorage

main.js

//保存字典数据到LocalStorage
Vue.prototype.$api.setting.SystemDictType.all().then(({data}) => {
	loadDictsToLocalStorage(data)
})

新增 dictionary.js 放在 Utils文件夹里面


// 获取字典数据
export function getDictByType(dictType) {
    const dicts = JSON.parse(localStorage.getItem('dicts') || '[]');
    // 检查 dicts 是否是数组并且非空
    if (Array.isArray(dicts) && dicts.length > 0) {
        return dicts.find(dict => dict.dictType === dictType);
    }
    console.error('字典数据格式不正确或为空');
    return null;  // 返回 null 或一个默认值
}


// 根据字典类型和值获取字典标签
export function getDictLabel(dictType, dictValue) {
    const dict = getDictByType(dictType);
    if (dict) {
        const item = dict.dataList.find(i => i.dictValue === dictValue);
        return item ? item.dictLable : '';
    }
    return '';  // 如果没有找到标签,返回空字符串
}


// 初始化时将字典数据存储到 localStorage
export function loadDictsToLocalStorage(dictData) {
    //清除数据
    localStorage.removeItem('dicts');
    // 将字典数据转换成 JSON 字符串并存储
    localStorage.setItem('dicts', JSON.stringify(dictData));


}

页面使用

<Table :datas="datas" ref="table" :border="true">
	<TableItem title="事件状态" :render="messageRender"></TableItem>
</Table>

methods: {
    messageRender(data) {
      return getDictLabel('eventStatus', data.status.toString())
    }
  }

相关文章:

  • 若依按照时间段查询
  • 开源且免费的CMS系统有哪几个可以放心用?
  • 自动化之ansible(二)
  • 【反馈-建立负反馈,警惕正反馈-关键字摘要】
  • 【详细讲解在STM32的UART通信中使用DMA机制】
  • 23.4 MSHTML组件
  • pycharm 调试 debug 进入 remote_sources
  • C/C++高性能Web开发框架全解析:2025技术选型指南
  • CompletableFuture 使用和源码解读
  • JAVA版本游戏进程读写操作
  • Java 大视界 -- 国际竞争与合作:Java 大数据在全球市场的机遇与挑战(94)
  • 网络安全运维服务手册 运维网络安全相关知识
  • hot100_70. 爬楼梯
  • MTK-Android13-包安装器PackageInstaller 静默安装实现
  • Python之装饰器三 踩坑(带参数,不带参数,两者都带参数)
  • SpringBoot线程池的使用
  • QML 实现一个动态的启动界面
  • Apipost和Apifox如何选型(1):WebSocket调试
  • 【Elasticsearch】多索引(数据流)搜索
  • conda 配置源
  • 破题“省会担当”,南京如何走好自己的路?
  • “敌人已经够多了”,菲总统马科斯:愿与杜特尔特家族和解
  • 人民日报:不能层层加码,要层层负责
  • 夜读丨为萤火虫哭泣的夜晚
  • 张宇祥已任上海闵行区委常委、副区长
  • 深一度|上座率连创纪录撬动文旅,中超可否复制大连模式