日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

React樣式?jīng)_突問題

作者:qq_45689385 更新時間: 2022-07-21 編程語言

前言:因為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

欄目分類
最近更新