reactjs - css-loader 在导入 css 文件时返回未定义

标签 reactjs webpack css-loader css-modules webpack-loader

我有一个名为 App.css 的 css 文件,其中的 css 类似于

.abcd {
    margin-top: 50px !important;
    color: red !important;
}

我正在导入它并在我的 react 应用程序中使用

import styles from './App.css'
console.log("style", styles) // it gives object. 

<div className={"ui equal width grid " + styles.abcd}>
</div>

这里styles.abcd显示未定义,但是当我控制台styles时,它在数组上给出了具有该类名的对象。我不知道什么此处缺失。

我的 webpackconf 看起来像:

{
    test: /\.css$/,
    loader: 'css-loader',
    options: {
        modules: true,
        localIdentName: '[path][name]__[local]--[hash:base64:5]',
    },
},

我也尝试过

{
    test: /\.css$/,
    use: ['style-loader', 'css-loader'],
},

没有任何效果。为什么它给出未定义

最佳答案

类似问题在这里得到解答:CSS Modules class names undefined

In the docs, it's mentioned that you have to name the file as follow :

[name].module.css

Try renaming the file to : Catalog.module.css and import it like this :

import styles from './Catalog.module.css';

在您的情况下,您应该将其命名为 App.module.css 并照此导入。

关于reactjs - css-loader 在导入 css 文件时返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53726341/

相关文章:

css - 无法解析 'css-loader' - Webpack 入门错误

javascript - 使用 redux saga 和 redux 表单在 React 应用程序中显示表单错误

javascript - 让目录中的所有文件成为 webpack 入口点的最佳方法?

javascript - React 构建多个文件

webpack - 将 eslint-webpack-plugin 添加到项目中以提供 Typescript linting

javascript - Webpack 4 css-loader 无法解析字体

javascript - 子组件在 POST 请求后不重新渲染父组件

javascript - 使用ReactJS和Material-UI的Fake Select占位符

javascript - React/Jest 错误 : services. map is not a function

javascript - Vue.js 项目 - CSS 模块的模块构建失败