vue3 ts 不使用 if switch 来决定使用什么函数

const handleOperation = (icon: string) => {

  const handleSetting = () => {
    console.log("handleSetting");
  };

  const handleDataAnalysis = () => {
    console.log("handleDataAnalysis");
  };

  const handlers: { [key: string]: () => void } = {
    "Setting": handleSetting,
    "DataAnalysis": handleDataAnalysis
  };

  const handler = handlers[icon];
  handler ? handler() : (() => {
    throw new Error("No handler found for icon: " + icon);
  })();

};

相关推荐

  1. vue3 ts 使用 if switch 决定使用什么函数

    2024-07-11 12:26:01       23 阅读
  2. Vue3 ts使用echarts

    2024-07-11 12:26:01       41 阅读
  3. vue3+ts 使用WebSocket

    2024-07-11 12:26:01       29 阅读
  4. 初学vue3ts:keep-alive的简单使用

    2024-07-11 12:26:01       54 阅读

最近更新

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

    2024-07-11 12:26:01       102 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 12:26:01       109 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 12:26:01       91 阅读
  4. Python语言-面向对象

    2024-07-11 12:26:01       98 阅读

热门阅读

  1. 拖动未来:WebKit 完美融合拖放API的交互艺术

    2024-07-11 12:26:01       29 阅读
  2. helm安装解决无授权问题

    2024-07-11 12:26:01       27 阅读
  3. php获取,昨,今,后天.... 本周,月,年...日期时间戳

    2024-07-11 12:26:01       29 阅读
  4. 大话C语言:第28篇 内存分配与释放

    2024-07-11 12:26:01       23 阅读
  5. MySQL 中删除重复的数据并只保留一条

    2024-07-11 12:26:01       26 阅读
  6. spring boot 3.2.x 使用CDS加速启动

    2024-07-11 12:26:01       31 阅读
  7. 37.深度学习中的梯度下降法及其实现

    2024-07-11 12:26:01       28 阅读
  8. Spring Boot与Spring MVC的区别和联系

    2024-07-11 12:26:01       33 阅读
  9. 代码随想录-DAY⑥-哈希表——leetcode 383 | 454

    2024-07-11 12:26:01       27 阅读
  10. linux去掉行首的#字符

    2024-07-11 12:26:01       25 阅读
  11. 常见的负载均衡算法和实现方式

    2024-07-11 12:26:01       28 阅读
  12. Android焦点之Focused Window的更新(二)

    2024-07-11 12:26:01       24 阅读
  13. SpringBoot源码阅读(9)——转换服务

    2024-07-11 12:26:01       22 阅读
  14. C#中的Dictionary

    2024-07-11 12:26:01       26 阅读
  15. C语言标准库中的函数

    2024-07-11 12:26:01       31 阅读