reactjs - React.js 中的 CSS 模块不加载
问题描述
我正在学习 React.js,并且正在使用 Udemy 课程。问题是,自从课程制作到现在,React 已经更新,CSS 模块也发生了变化。我试图进行研究并编写代码,但这些类似乎不适用。我在浏览器中检查了检查,在 React 选项卡下(安装了扩展程序)它说 className={undefined}。在检查器选项卡下它说只是
<div>...</div>
没有应用任何课程。我将 css 文件命名为 [component].module.css
这是一个组件,我做的都是一样的,都有这个问题:
import React from 'react';
// import Aux from '../../hoc/Aux';
import Ingredient from './Ingreditent/Ingredient';
import styles from './Burger.module.css';
const burger = props => (
<div className={styles.Burger}>
<Ingredient type='bread-top' />
<Ingredient type='meat'/>
<Ingredient type='bread-bottom' />
</div>
);
export default burger;
这是 CSS 文件 Burger.module.css:
.Burger{
width: 50vw;
height: 30vh;
margin: auto;
overflow: scroll;
text-align: center;
font-size: 1.2rem;
font-weight: bold;
}
@media (min-width: 1000px) and (min-height: 700px){
.Burger{
width: 70vw;
height: 30vw;
}
}
编辑:webpack.config.dev.js 的第 143 到 150 行:文件在这里https://1drv.ms/u/s!AuFJzTL8l8Op9g5CBJ8kv8Wv-pcx
test: /\.css$/,
options: {
importLoaders: 1,
modules: true,
localIdentName: '[name]__[local]__[hash:base64:5]'
},
loader: 'style!css?importLoaders=1!postcss'
},
解决方案
在新版本的 react 中应该是这样的:
test: cssRegex,
exclude: cssModuleRegex,
use: getStyleLoaders({
modules: true,
modules: {
localIdentName: "[name]__[local]__[hash:base64:5]",
},
importLoaders: 1,
旧版:
test: /\.css$/,
optio`enter code here`ns: {
importLoaders: 1,
modules: true,
localIdentName: '[name]__[local]__[hash:base64:5]'
},
推荐阅读
- python - 区分python文件,忽略行尾样式、缩进样式和尾随空格
- java - 声明一个已经创建的 JDialog
- python - 如何用其他数据框的值替换熊猫中的整个单元格并将其余部分设置为1?
- angular - 我无法将一个模块中的组件用于另一个模块组件
- python - AWS Lambda 在私有 API 网关后面请求另一个 lambda - DNS 解析不起作用
- c# - 在 ASP .Net Core 3.0 中使用 Heroku PostgreSql
- bit-manipulation - 表达式“x & (x + (1 << n))”是如何工作的?
- python - Windows长度矩阵转向量,提取数据
- javascript - 如何在 Symfony 项目中使用 jQuery 和 webpack-encore?
- javascript - React.js - 无法访问对象数组中的值