用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瀏覽器