網(wǎng)站首頁 編程語言 正文
前言:因為react最終編譯打包后都在一個html頁面中,如果在兩個組件中取一樣類名分別引用在自身,那么后者會覆蓋前者
例如 組件Hello
jsx:
import React,{Component} from 'react'
import './index.css'
export default class Hello extends Component{
render(){
return <h2 className={title}>Hello,React!</h2>
}
}
css:
.title{
background-color: orange;
}
組件Welcome
jsx:
import React,{Component} from 'react'
import './index.css'
export default class Welcome extends Component{
render(){
return <h2 className={title}>Hello,React!</h2>
}
}
css:
.title{
background-color: skyblue;
}
兩者會沖突,只會取其中一個
解決方法 一
css文件后綴前面加上module 例如 index.module.css
然后引入
import React,{Component} from 'react'
import hello from './index.module.css'
export default class Hello extends Component{
render(){
return <h2 className={hello.title}>Hello,React!</h2>
}
}
方法 二?
可以在最外層寫個class類名,樣式嵌套寫法,可以使用less和sass
jsx:
import React,{Component} from 'react'
import hello from './index.css'
export default class Hello extends Component{
render(){
return (
<div className={hello}>
<h2 className={title}>Hello,React!</h2>
</div>
)
}
}
css:
.hello{
.title{
background-color: orange;
}
}
原文鏈接:https://blog.csdn.net/qq_45689385/article/details/124230679
- 上一篇:react中的redux
- 下一篇:React生命周期
相關(guān)推薦
- 2022-03-17 C++游戲編程之模擬實現(xiàn)鍵盤打字程序_C 語言
- 2022-04-11 K8S部署Kafka界面管理工具(kafkamanager)方法詳解_云其它
- 2023-04-26 C++利用伴隨陣法實現(xiàn)矩陣求逆_C 語言
- 2022-09-05 SparkStreaming寫入Hive慢
- 2022-08-28 隔代獲取dom,多個commit合并成一個,計算屬性完整寫法
- 2022-04-01 SQL?Server的存儲過程詳解_MsSql
- 2023-02-06 python?wordcloud庫實例講解使用方法_python
- 2022-03-03 sublime-text - Sublime Text 3,在注釋的下一行,如何取消自動注釋?
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支