【问题标题】:How to import Ant Design components without default class names?如何导入没有默认类名的 Ant Design 组件?
【发布时间】:2020-01-23 23:23:15
【问题描述】:

我在我的反应项目中使用Ant Design。我想使用没有默认类名的 Ant Design 组件。

注意以下代码:

import { Typography } from "antd";

const { Title } = Typography;

~~

<Title level={4}> ...

原因如下:

<h4 class="ant-typography" ...

还有它的 CSS:

// in the less file:
h4.ant-typography {
    // some css
}

所以当我将我的类名添加到组件时,Ant Design 组件的类名的优先级高于我的!

我的班级名称是.root-23-07。此优先级编号为10,但由于在less 文件中使用标签名称,And Design CSS 选择的优先级编号为11。所以 Ant Design 样式会覆盖我的样式。

我想使用没有默认类名的 And Design 组件。 有可能吗?

【问题讨论】:

  • 你不能覆盖 CSS 吗?检查thisthis
  • 你不能,如果你能这样做,它就没有意义。您可以覆盖它们。
  • 你询问了 CSS 而没有提到 JSS

标签: css reactjs antd


【解决方案1】:

如果我正确理解了您的问题,不,如果没有基础类名,您将无法使用 antd。因为 React 输出原始 html 和 css,没有类名将导致在您提供的 css 之外没有任何样式。

您需要使您的类与特异性级别相匹配,或者使用!important

【讨论】:

  • 感谢您的努力,亲爱的 Matthew,使用 !important 是我解决此问题的最后一个解决方案,我这样做是为了发布项目。但我看到我们可以像 import Title from "antd/es/typography/Title"import Title from "antd/lib/typography/Title" 一样导入 antd 组件。我猜可能这个导入不包含样式。
  • 导入组件的方式不会影响样式。有两种加载样式的方法,您可以使用 import 'antd/dist/antd.css';import 'antd/es/button/style'; 之类的单个组件加载所有 antd css。无论哪种方式,一旦您导入这些样式,它们将存在于应用程序中的所有组件中。理论上你不能导入 antd css,但是你必须自己为所有组件编写样式。
猜你喜欢
  • 2020-02-21
  • 2018-07-02
  • 2019-10-08
  • 2020-04-08
  • 1970-01-01
  • 2019-10-22
  • 2021-11-28
  • 1970-01-01
  • 2022-07-15
相关资源
最近更新 更多