Stu*_*tje 10 css css-modules react-css-modules
CardComponent假设我们有这样的第三方
<Card title="This is a title in the header">This is text in the body</Card>
Run Code Online (Sandbox Code Playgroud)
这会以纯 HTML 形式呈现
<div class="Card">
<div class="CardHeader"></div>
<div class="CardBody"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
我使用 css 模块进行样式设置(在本例中为 scss)。
.customCard {
.CardBody {
color: red;
}
}
Run Code Online (Sandbox Code Playgroud)
然后将类添加到Card中
import styles from ...
...
<Card className={styles.customCard} ...>...</Card>
Run Code Online (Sandbox Code Playgroud)
上面将不起作用,因为它为CardBodylike创建了一个生成的类CardBody_sjkdh43。有没有办法在模块中选择非生成的类名?或者这只能以旧时尚的方式使用样式表?
Clo*_*ery 19
答案是使用:global()CSS 模块中的选择器。
欲了解更多信息:异常 - CSS 模块
代码示例:
<div class={styles.myClass}>
<SomeComponentWithOwnCss />
</div>
Run Code Online (Sandbox Code Playgroud)
输出:
<div class="myClass_s4jkdh3">
<div class="SomeComponentWithOwnCss"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
和CSS:
.myClass div:global(.SomeComponentWithOwnCss) {
background-color: red;
}
Run Code Online (Sandbox Code Playgroud)
这里的工作示例:codesandbox