React中设置样式
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px">使用一个小例子来演示React中样式的使用,如下图,做一个静态的评论列表组件:</span></p><p><img src="https://img2018.cnblogs.com/i-beta/1813051/201912/1813051-20191212160403092-2123089110.png"></p>
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px"> <strong>1.创建两个JSX文件:</strong>CmtItem.jsx和CmtList.jsx</span></p>
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px">CmtList.jsx:</span></p>
<div class="cnblogs_code">
<pre>import React from 'react'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">导入评论项子组件</span>
import CmtItem from '@/components/CmtItem'
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使用class关键字定义父组件</span>
export <span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> class CmtList extends React.Component{
constructor(){
super();
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.state =<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">这是评论列表数据</span>
<span style="color: rgba(0, 0, 0, 1)"> CommentList: [
{id: </span>1, user: "张三", content: "哈哈,沙发"<span style="color: rgba(0, 0, 0, 1)"> },
{id: </span>2, user: "李四", content: "哈哈,板凳"<span style="color: rgba(0, 0, 0, 1)"> },
{id: </span>3, user: "王五", content: "哈哈,凉席"<span style="color: rgba(0, 0, 0, 1)"> },
{id: </span>4, user: "赵六", content: "哈哈,砖头"<span style="color: rgba(0, 0, 0, 1)"> },
{id: </span>5, user: "田七", content: "哈哈,热炕头"<span style="color: rgba(0, 0, 0, 1)"> }
]
}
}
render(){
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <div>
<h1>这是评论列表组件</h1>
{<span style="color: rgba(0, 0, 255, 1)">this</span>.state.CommentList.map(item =><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <CmtItem {...item} key={item.id}></CmtItem>
<span style="color: rgba(0, 0, 0, 1)"> })}
</span></div>
<span style="color: rgba(0, 0, 0, 1)"> }
}</span></pre>
</div>
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px">CmtItem.jsx:</span></p>
<div class="cnblogs_code">
<pre>import React from 'react'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使用function构造函数定义普通的无状态组件</span>
export <span style="color: rgba(0, 0, 255, 1)">default</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> CmtItem(props){
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <div>
<h1>评论人:{props.user}</h1>
<p>评论内容:{props.content}</p>
</div>
}</pre>
</div>
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px">index.js:</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1.导入包</span>
import React from 'react'<span style="color: rgba(0, 0, 0, 1)">
import ReactDOM from </span>'react-dom'
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">导入评论项子组件</span>
import CmtList from '@/components/CmtList'<span style="color: rgba(0, 0, 0, 1)">
ReactDOM.render(</span><div>
<CmtList></CmtList>
</div>,document.getElementById('app'));</pre>
</div>
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px">主要的框架已经搭建好了,如下图:</span></p>
<p><img src="https://img2018.cnblogs.com/i-beta/1813051/201912/1813051-20191212162503743-834371295.png"></p>
<p><strong><span style="font-family: "comic sans ms", sans-serif; font-size: 16px"> 2.现在要给该列表组件添加样式:</span></strong></p>
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px"><strong>(1)使用普通的style样式</strong>,类似以下这种(注意与HTML标签使用样式的区别):</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">h1 </span><span style="color: rgba(255, 0, 0, 1)">style </span><span style="color: rgba(0, 0, 255, 1)">= { </span><span style="color: rgba(255, 0, 0, 1)">{ color: 'red', fontWeight: 200 } } </span><span style="color: rgba(0, 0, 255, 1)">></</span><span style="color: rgba(128, 0, 0, 1)">h1</span><span style="color: rgba(0, 0, 255, 1)">></span></pre>
</div>
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px">下面是HTML标签的语法,注意区别:</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">h1 </span><span style="color: rgba(255, 0, 0, 1)">style </span><span style="color: rgba(0, 0, 255, 1)">= " color: red; font-weight: 200 "</span> <span style="color: rgba(0, 0, 255, 1)">></</span><span style="color: rgba(128, 0, 0, 1)">h1</span><span style="color: rgba(0, 0, 255, 1)">></span></pre>
</div>
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px"><strong>(2)将样式对象和UI结构分离</strong>,例如:</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">第一层封装方式:将样式对象和UI结构分离</span>
const itemStyle = {border: '1px dashed #ccc', margin: '10px', padding: '10px', boxShadow: '0 0 #ccc'<span style="color: rgba(0, 0, 0, 1)">};
const userStyle </span>= {fontSize: '14px'<span style="color: rgba(0, 0, 0, 1)">};
const contentStyle </span>= {fontSize: '12px'<span style="color: rgba(0, 0, 0, 1)">};
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使用function构造函数定义普通的无状态组件</span>
export <span style="color: rgba(0, 0, 255, 1)">default</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> CmtItem(props){
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <div style = {itemStyle}>
<h1 style = {userStyle}>评论人:{props.user}</h1>
<p style = {contentStyle}>评论内容:{props.content}</p>
</div>
}</pre>
</div>
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px"><strong>(3)合并成一个大的样式对象</strong>,例如:</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">第二种封装方式:合并成一个大的样式对象</span>
const styles =<span style="color: rgba(0, 0, 0, 1)"> {
item: {border: </span>'1px dashed #ccc', margin: '10px', padding: '10px', boxShadow: '0 0 #ccc'<span style="color: rgba(0, 0, 0, 1)">},
user: {fontSize: </span>'14px'<span style="color: rgba(0, 0, 0, 1)">},
content: {fontSize: </span>'12px'<span style="color: rgba(0, 0, 0, 1)">}
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使用function构造函数定义普通的无状态组件</span>
export <span style="color: rgba(0, 0, 255, 1)">default</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> CmtItem(props){
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <div style = {styles.item}>
<h1 style = {styles.user}>评论人:{props.user}</h1>
<p style = {styles.content}>评论内容:{props.content}</p>
</div>
}</pre>
</div>
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px"><strong>(4)抽离为单独的样式模块:</strong>创建一个style.js文件:</span></p>
<div class="cnblogs_code">
<pre>export <span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)">{
item: {border: </span>'1px dashed #ccc', margin: '10px', padding: '10px', boxShadow: '0 0 10px #ccc'<span style="color: rgba(0, 0, 0, 1)">},
user: {fontSize: </span>'14px'<span style="color: rgba(0, 0, 0, 1)">},
content: {fontSize: </span>'12px'<span style="color: rgba(0, 0, 0, 1)">}
}</span></pre>
</div>
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px">然后再调用该文件(例如,CmtItem.jsx:):</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">第三种封装方式:抽离为单独的样式模块</span>
import styles from '@/components/styles.js'
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使用function构造函数定义普通的无状态组件</span>
export <span style="color: rgba(0, 0, 255, 1)">default</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> CmtItem(props){
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <div style = {styles.item}>
<h1 style = {styles.user}>评论人:{props.user}</h1>
<p style = {styles.content}>评论内容:{props.content}</p>
</div>
}</pre>
</div>
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px"><strong>(5)创建样式表文件:</strong>cmtlist.css文件,首先需要安装style-loader和css-loader并启用css-modules:</span></p>
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px"> ●修改webpack.config.js配置文件,为css-loader添加参数</span></p>
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px"> ●在需要的组件中,import导入样式表,并接收模块化的CSS样式对象</span></p>
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px"> ●在需要的HTML标签上,使用className指定模块化的CSS样式对象</span></p>
<div class="cnblogs_code">
<pre>module: { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">所有第三方模块的配置规则</span>
<span style="color: rgba(0, 0, 0, 1)"> rules: [
{test: </span>/\.js|jsx$/, use:'babel-loader', exclude: /node_modules/},<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">注意:在配置babel-loader时,一定要加上exclude: /node_modules/,否则整个项目会报错</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">可以在css-loader之后通过?追加参数</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">其中有个固定的参数modules,表示为普通的CSS样式表启用模块化</span>
{test: /\.css$/, use: ['style-loader', 'css-loader?modules']}, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打包处理css样式表的第三方loader </span>
<span style="color: rgba(0, 0, 0, 1)"> ]
}</span></pre>
</div>
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px">样式表cmtlist.css:</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 0, 1)">.title</span>{<span style="color: rgba(255, 0, 0, 1)">
color</span>:<span style="color: rgba(0, 0, 255, 1)"> red</span>;<span style="color: rgba(255, 0, 0, 1)">
font-size</span>:<span style="color: rgba(0, 0, 255, 1)"> 35px</span>;<span style="color: rgba(255, 0, 0, 1)">
font-weight</span>:<span style="color: rgba(0, 0, 255, 1)"> 200</span>;<span style="color: rgba(255, 0, 0, 1)">
text-align</span>:<span style="color: rgba(0, 0, 255, 1)"> center</span>;
}
<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">css模块化只针对 类选择器(例如.title) 和 ID选择器(例如#id)生效,不会将标签选择器模块化</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(128, 0, 0, 1)">
.user</span>{<span style="color: rgba(255, 0, 0, 1)">
font-size</span>:<span style="color: rgba(0, 0, 255, 1)"> 14px</span>;
}<span style="color: rgba(128, 0, 0, 1)">
.content</span>{<span style="color: rgba(255, 0, 0, 1)">
font-size</span>:<span style="color: rgba(0, 0, 255, 1)"> 12px</span>;
}<span style="color: rgba(128, 0, 0, 1)">
.cmtbox</span>{<span style="color: rgba(255, 0, 0, 1)">
border</span>:<span style="color: rgba(0, 0, 255, 1)"> 1px dashed #ccc</span>;<span style="color: rgba(255, 0, 0, 1)">
margin</span>:<span style="color: rgba(0, 0, 255, 1)"> 10px</span>;<span style="color: rgba(255, 0, 0, 1)">
padding</span>:<span style="color: rgba(0, 0, 255, 1)"> 10px</span>;<span style="color: rgba(255, 0, 0, 1)">
box-shadow</span>:<span style="color: rgba(0, 0, 255, 1)"> 0 0 10px #ccc</span>;
}
<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> 被 :global 包裹起来的类名不会被css-modules模块化,而是会全局生效
被 :local 包裹起来的类名会被模块化,默认情况下所有的类名和ID都是被模块化的 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(128, 0, 0, 1)">
:global(.test)</span>{<span style="color: rgba(255, 0, 0, 1)">
font-style</span>:<span style="color: rgba(0, 0, 255, 1)"> italic</span>;
}</pre>
</div>
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px">调用样式表,例如,CmtItem.jsx::</span></p>
<div class="cnblogs_code">
<pre>import React from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import cssobj from </span>'@/css/cmtlist.scss'<span style="color: rgba(0, 0, 0, 1)">
console.log(cssobj); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果没有启用css-modules模块化,则cssobj只是一个空对象,下面的引用样式的方式应该是className="样式名"</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使用function构造函数定义普通的无状态组件</span>
export <span style="color: rgba(0, 0, 255, 1)">default</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> CmtItem(props){
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <div className={cssobj.cmtbox}>
<h1 className={cssobj.user}>评论人:{props.user}</h1>
<p className={cssobj.content}>评论内容:{props.content}</p>
</div>
}</pre>
</div>
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px">css模块化后控制台输出的cssobj对象的内容如下:类名的值是随机码,如果想要自定义生成类名格式,可以使用localIdentName</span></p>
<p><img src="https://img2018.cnblogs.com/i-beta/1813051/201912/1813051-20191212171501603-1601719619.png"></p>
<p> <span style="font-family: "comic sans ms", sans-serif; font-size: 16px">使用localIdentName自定义生成类名格式,可选的参数有:</span></p>
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px"> ● 表示样式表相对于项目根目录所在的路径</span></p>
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px"> ● 表示样式表文件名称</span></p>
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px"> ● 表示样式的类名定义名称</span></p>
<p><span style="font-family: "comic sans ms", sans-serif; font-size: 16px"> ● 表示32位的hash值</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">css配置如下</span>
{test: /\.css$/<span style="color: rgba(0, 0, 0, 1)">, use: [
{
loader: </span>'style-loader'<span style="color: rgba(0, 0, 0, 1)">
},
{
loader: </span>'css-loader'<span style="color: rgba(0, 0, 0, 1)">,
options: {
modules: {
localIdentName: </span>"--"<span style="color: rgba(0, 0, 0, 1)">
}
}
},
{
loader: </span>'sass-loader'<span style="color: rgba(0, 0, 0, 1)">
}
]} </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打包处理scss文件的loader</span></pre>
</div>
<p><img src="https://img2018.cnblogs.com/i-beta/1813051/201912/1813051-20191212172346063-126085896.png"></p>
<p> </p><br><br>
来源:https://www.cnblogs.com/zcy9838/p/12023168.html
頁:
[1]