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

纷析云开源版- 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())
    }
  }

http://www.kler.cn/a/559824.html

相关文章:

  • 【Python爬虫(60)】解锁社交媒体数据宝藏:Python爬虫实战攻略
  • buuctf-[极客大挑战 2019]LoveSQL
  • 调试无痛入手
  • 蓝桥杯试题:小明的彩灯(差分 前缀和)
  • Linux系统移植之Uboot启动流程
  • “国补”带火手机换新,出售旧手机应如何保护个人信息安全
  • 鸿蒙学习-
  • Http升级为Https - 开发/测试服环境
  • 基于物联网的家庭版防疫面罩设计与实现(论文+源码)
  • μEMU部署测试(论文复现)
  • 整理好的java面试八大常用算法
  • 宠物智能可穿戴产品调研报告
  • QT开发:事件循环与处理机制的概念和流程概括性总结
  • 详解Tomcat下载安装以及IDEA配置Tomcat(2023最新)
  • Spring Boot中如何使用Thymeleaf模板引擎
  • npm使用了代理,但是代理软件已经关闭导致创建失败
  • 使用 AIStor 和 OpenSearch 增强搜索功能
  • 基于Spring Boot的公司资产网站设计与实现(LW+源码+讲解)
  • 高清下载油管视频到本地
  • 【MySQL篇】数据库基础