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

如何在组件中引用MantineProvider全局样式中的自定义CSS类?

解决Mantine全局样式类引用问题

问题原因

你在globalStyles中定义的.text-blue是全局CSS类,不属于Mantine主题对象的属性。你错误地尝试通过theme.text-blue或theme.text_blue引用它,这两种写法都不符合语法逻辑:

  • theme.text-blue会被JavaScript解析为theme.text减去blue,导致类型不匹配错误
  • theme.text_blue并非Mantine主题内置属性,所以触发不存在的属性报错

正确引用方式

直接将类名字符串传入className属性即可,因为全局样式已经注入到页面全局作用域:

<Title className={classes.title} order={2}>
  A little bit <span className="text-blue">about us</span>
</Title>

补充方案(动态主题色)

如果需要在组件中直接使用主题颜色值(无需依赖全局类),可以通过useMantineTheme钩子获取主题配置,再用内联样式实现:

import { useMantineTheme } from '@mantine/core';

function AboutSection() {
  const theme = useMantineTheme();
  const blueColor = theme.colorScheme === 'dark' ? theme.colors.blue[5] : theme.colors.blue[7];
  
  return (
    <Title className={classes.title} order={2}>
      A little bit <span style={{ color: blueColor }}>about us</span>
    </Title>
  );
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 14:40:49