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

學(xué)無(wú)先后,達(dá)者為師

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

React樣式?jīng)_突問(wèn)題

作者:qq_45689385 更新時(shí)間: 2022-07-21 編程語(yǔ)言

前言:因?yàn)閞eact最終編譯打包后都在一個(gè)html頁(yè)面中,如果在兩個(gè)組件中取一樣類名分別引用在自身,那么后者會(huì)覆蓋前者

例如 組件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;
}

兩者會(huì)沖突,只會(huì)取其中一個(gè)

解決方法 一

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>
	}
}

方法 二?

可以在最外層寫個(gè)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

欄目分類
最近更新