You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Material UI中第三个图标?代码报错求助

解决Material UI Drawer列表项图标替换的语法错误问题

你的代码报错核心是三元表达式语法错误——标准三元运算符的结构是 条件 ? 真值内容 : 假值内容,你额外多写了一个: <MailIcon />,导致JS解析失败。

修正方案分两种场景:

  • 场景1:仅第三个列表项(index=2)用GroupIcon,其余用LibraryMusicIcon
    直接修正三元表达式,去掉多余的冒号和图标:

    {index === 2 ? <GroupIcon /> : <LibraryMusicIcon />}
    
  • 场景2:需要多个条件匹配不同图标(比如第一个用某图标、第三个用GroupIcon、其他用MailIcon)
    推荐用可读性更高的函数+switch写法,避免多层嵌套三元的混乱:

    // 先定义图标匹配函数
    const getDrawerIcon = (index) => {
      switch(index) {
        case 0:
          return <你的第一个图标组件 />;
        case 2:
          return <GroupIcon />;
        default:
          return <MailIcon />;
      }
    };
    
    // 在Drawer列表项中调用
    {getDrawerIcon(index)}
    

额外注意:

确保所有用到的Material UI图标都已正确导入,比如:

import GroupIcon from '@mui/icons-material/Group';
import LibraryMusicIcon from '@mui/icons-material/LibraryMusic';
import MailIcon from '@mui/icons-material/Mail';

内容的提问来源于stack exchange,提问作者Prab

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 05:50:25