前端代码优化--computed

随便记录一下

主要是通过计算属性来简化和优化代码。在 Vue 中,计算属性是一种方便的工具,可以让你根据依赖状态的变化来动态计算衍生值。在这个例子中,我们使用计算属性 formattedCommunicationType 来根据 workDetail.realTimeItemDeviceDTO.communicationType 的值动态计算通信类型的显示文本,从而避免了重复的逻辑和代码。

通过使用计算属性,我们能够更清晰地表达代码的意图,并且在需要时自动更新计算结果。这样可以提高代码的可读性和可维护性,同时减少重复代码的使用。

旧代码

   <div class="mode-top">
                <span
                  v-if="
                    workDetail.realTimeItemDeviceDTO.communicationType &&
                    workDetail.realTimeItemDeviceDTO.communicationType == '4G'
                  "
                >
                  {{
                    workDetail.realTimeItemDeviceDTO.communicationType
                      ? workDetail.realTimeItemDeviceDTO.communicationType
                      : '--'
                  }}
                </span>
                <span
                  style="font-size: 10px"
                  v-else-if="
                    workDetail.realTimeItemDeviceDTO.communicationType &&
                    workDetail.realTimeItemDeviceDTO.communicationType == 'WIFi'
                  "
                >
                  {{
                    workDetail.realTimeItemDeviceDTO.communicationType
                      ? workDetail.realTimeItemDeviceDTO.communicationType
                      : '--'
                  }}
                </span>
                <span
                  v-else-if="
                    workDetail.realTimeItemDeviceDTO.communicationType &&
                    workDetail.realTimeItemDeviceDTO.communicationType == 'Lan'
                  "
                >
                  {{
                    workDetail.realTimeItemDeviceDTO.communicationType
                      ? workDetail.realTimeItemDeviceDTO.communicationType
                      : '--'
                  }}
                </span>
                <span
                  v-else-if="
                    workDetail.realTimeItemDeviceDTO.communicationType &&
                    workDetail.realTimeItemDeviceDTO.communicationType == 'Nb_lot'
                  "
                >
                  {{
                    workDetail.realTimeItemDeviceDTO.communicationType
                      ? workDetail.realTimeItemDeviceDTO.communicationType.slice(0, 2)
                      : '--'
                  }}
                </span>
                <span v-else> -- </span> 
               
              </div>

这样写是因为刚开始它嵌套没有这么多,直接.xxx就可以拿到,现在是.realTimeItemDeviceDTO.xxx才能拿到,并且刚开始不知道它会有多种通讯模式,加上ui设计,它设计是这样的,如果字数太多还要截取的情况,就太繁琐,要改,

新代码,这样就好了


const communicationTypes: Record<string, string> = {
  '4G': '4G',
  WiFi: 'WIFI',
  Lan: 'Lan',
  Nb_lot: 'Nb',
};

const formattedCommunicationType = computed(() => {
  const type = workDetail.value.realTimeItemDeviceDTO?.communicationType;
  return type ? communicationTypes[type] || '--' : '--';
});
<div class="mode-top">
    <span style="font-size: 10px">{{ formattedCommunicationType }}</span>
</div>

相关推荐

  1. 前端代码优化

    2024-04-12 19:28:04       36 阅读
  2. 前端代码优化-switch的使用

    2024-04-12 19:28:04       35 阅读
  3. <span style='color:red;'>Computers</span>

    Computers

    2024-04-12 19:28:04      52 阅读
  4. computed

    2024-04-12 19:28:04       40 阅读
  5. 前端各种优化

    2024-04-12 19:28:04       49 阅读
  6. 前端如何优化工程

    2024-04-12 19:28:04       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-12 19:28:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-12 19:28:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-12 19:28:04       87 阅读
  4. Python语言-面向对象

    2024-04-12 19:28:04       96 阅读

热门阅读

  1. 登录界面前端

    2024-04-12 19:28:04       38 阅读
  2. 期货开户长线好还是短线好?

    2024-04-12 19:28:04       43 阅读
  3. python内置函数dir()、divmod()详解

    2024-04-12 19:28:04       41 阅读
  4. DFL在网络安全审计中的应用研究的开题报告

    2024-04-12 19:28:04       35 阅读
  5. 对用户上传图片进行压缩

    2024-04-12 19:28:04       37 阅读
  6. 请求的数据类型{ }{[ ]} 解析

    2024-04-12 19:28:04       43 阅读
  7. fastjson2 简单使用案例

    2024-04-12 19:28:04       43 阅读
  8. Qt安装 qt-unified-windows-x64-online.exe下载慢

    2024-04-12 19:28:04       34 阅读