你这样写className是不会同时生效多个类的:
className={styles.topToolItem, styles.toolWrap}
因为在JavaScript中,逗号运算符只会返回最后一个值,所以实际上只有 styles.toolWrap 会生效。
以下是正确的同时应用多个类的方法:
- 使用模板字符串(推荐):
className={`${styles.topToolItem} ${styles.toolWrap}`}
- 使用classnames库(适合条件类名):
import classNames from 'classnames';
className={classNames(styles.topToolItem, styles.toolWrap)}
- 使用数组join:
className={[styles.topToolItem, styles.toolWrap].join(' ')}
- 使用字符串拼接:
className={styles.topToolItem + ' ' + styles.toolWrap}
对于简单的静态类名,模板字符串(方法1)是最常用和最易读的方式。如果你需要根据条件动态添加类名,classnames库(方法2)是最好的选择。
例如,使用classnames处理条件类名:
className={classNames(
styles.topToolItem,
styles.toolWrap,
{
[styles.active]: isActive,
[styles.disabled]: isDisabled
}
)
这个修改应该在你的组件文件中进行,具体位置在需要应用多个类名的地方:
ChatInput.tsx
根据你的具体使用场景选择最合适的方法。对于简单的静态类名,模板字符串通常是最佳选择。
757

被折叠的 条评论
为什么被折叠?



