经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » React » 查看文章
在Create React App中使用CSS Modules的方法示例
来源:jb51  时间:2019/1/16 9:29:13  对本文有异议

前提条件

请先进行全局安装 create-react-app 插件哈,安装命令:npm install create-react-app -g

先使用 create-react-app 命令下载一个脚手架工程,安装命令:

  1. # 使用 npx
  2. $ npx create-react-app my-app
  3.  
  4. # 使用 npm
  5. $ npm init npx create-react-app my-app
  6.  
  7. # 使用 yarn
  8. $ yarn create react-app my-app

运行项目

  1. $ cd my-app
  2.  
  3. # 使用 npm
  4. $ npm start
  5.  
  6. # 或者使用yarn
  7. # yarn start

在浏览器中输入 http://localhost:3000 查看项目效果

使用 CSS Module 的第一种方式

create-react-app 中内置了使用 CSS Modules 的配置,当前方式就是使用 create-react-app 内置的用法

方式

将所有的 .css/.lee/.scss 等样式文件都修改成 .module.css/.module.less/.module.scss 等。即可使用 CSS Modules 的方式进行引入使用了。

用法

编写一个 css 文件:Button.module.css

  1. .error {
  2. background-color: red;
  3. }

在编写一个普通的 css 文件:another-stylesheet.css

  1. .error {
  2. color: red;
  3. }

在 js 文件中使用 CSS Modules 的方式进行引用:Button.js

  1. import React, { Component } from 'react';
  2. import styles from './Button.module.css'; // 使用 CSS Modules 的方式引入
  3. import './another-stylesheet.css'; // 普通引入
  4.  
  5. class Button extends Component {
  6. render() {
  7. // reference as a js object
  8. return <button className={styles.error}>Error Button</button>;
  9. }
  10. }

在浏览器中查看效果

此时 Button 组件的背景颜色是红色,但是字体颜色却不是红色,因为使用了 Css Modules 之后,普通的 css 样式就不起效果了,需要用全局的方式编写才可以(:global)。 最后添加到元素上的样式结果为: <button class="Button_error_ax7yz">Error Button</button>

使用 CSS Module 的第二种方式

方式

在命令行运行 npm run eject 命令

此命令会将脚手架中隐藏的配置都展示出来,此过程不可逆

运行完成之后,打开 config 目录下的 webpack.config.js 文件,找到 test: cssRegex 这一行

在 use 属性执行的方法中添加 modules: true ,如下图:

 

用法

和第一种方式的用法一致,只是不需要在 css 文件后面加 .module 后缀了

编写一个 css 文件:Button.css

  1. .error {
  2. background-color: red;
  3. }

再编写一个普通的 css 文件:another-stylesheet.css

  1. .error {
  2. color: red;
  3. }

在 js 文件中使用 CSS Modules 的方式进行引用:Button.js

  1. import React, { Component } from 'react';
  2. import styles from './Button.css'; // 可以直接使用 CSS Modules 的方式引入了
  3. import './another-stylesheet.css'; // 普通引入
  4.  
  5. class Button extends Component {
  6. render() {
  7. // reference as a js object
  8. return <button className={styles.error}>Error Button</button>;
  9. }
  10. }

在浏览器中查看效果

此时 Button 组件的背景颜色是红色,但是字体颜色却不是红色,因为使用了 Css Modules 之后,普通的 css 样式就不起效果了,需要用全局的方式编写才可以(:global)。 最后添加到元素上的样式结果为: <button class="Button_error_ax7yz">Error Button</button>

如想使用第二种方式对 sass 和 less 也使用 CSS Modules 的方式进行引用,则类似的在 sass 和 less 解析配置上也添加modules: true 即可。

注意

默认 create-react-app 脚手架不能直接使用 sass 和 less 直接编写 css,需要先进行相应配置。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号