用create-react-app 建立的项目不支持sass和less,须要手动配置css
npm install node-sass sass-loader --save
而后在config/webpack.config.js中添加如下代码,红色框的是新添加的代码:node
项目是采用create-react-app 搭建的脚手架工程如何应用CSS Modulereact
1.在命令行运行 npm run eject 命令
2.运行完成以后,打开 config 目录下的 webpack.config.js 文件,找到 test: cssRegex 这一行
3.在 use 属性执行的方法中添加 modules: true,以下图:webpack
CSS Moduleweb
$green: #14c3c3; .title { font-size: 20px; color: $green; }
在文件中的使用:npm
import React, { Component } from 'react' import style from './style.scss class index extends Component { render() { return <div className={style.title}>这是个人详细页面哦</div> } } export default index
CSS Module参考资料:https://www.jianshu.com/p/17fb9eb0a491浏览器