即使在弹出 create-react-app 后,css 模块也无法工作

标签 css reactjs webpack create-react-app css-modules

为了使用 css 模块,我弹出了我的 create-react-app。我从

修改了我的 webpack.config.dev.js
  {
            test: /\.css$/,
            use: [
              require.resolve('style-loader'),
              {
                loader: require.resolve('css-loader'),
                options: {
                  importLoaders: 1,
                    modules: true,           //added this
                    localIdentName: "[name]__[local]___[hash:base64:5]"  //added this
                },
              },
              {
                loader: require.resolve('postcss-loader'),
                options: {

                    // Necessary for external CSS imports to work
                  // https://github.com/facebookincubator/create-react-app/issues/2677
                  ident: 'postcss',
                  plugins: () => [
                    require('postcss-flexbugs-fixes'),
                    autoprefixer({
                      browsers: [
                        '>1%',
                        'last 4 versions',
                        'Firefox ESR',
                        'not ie < 9', // React doesn't support IE8 anyway
                      ],
                      flexbox: 'no-2009',
                    }),
                  ],
                },
              },
            ],
          }

然后我尝试在我的一个组件中使用 css 模块

import React, { Component } from 'react';

import styles from './sidebar.css';

class MenuPanel extends Component {
    render() {
        return (
            <div>
                <div className={styles.navbarSide}>
                    <div className="tessact-logo"></div>
                    <div className="navbar-item active">
                        <a className="navbar-item-link"><span className="fa fa-comment"></span> TRIVIA</a>
                    </div>

但是 className navbarSide 没有应用到 div。我看不到那个特定 div 的类名。我究竟做错了什么?理想情况下,div 应该有 className navbarSide。

最佳答案

只需添加 import { StyleSheet } from 'react-native';

import React, { Component } from 'react';
import { StyleSheet } from 'react-native';
import styles from './sidebar.css';

class MenuPanel extends Component {
    render() {
        return (
            <div>
                <div className={styles.navbarSide}>
                    <div className="tessact-logo"></div>
                    <div className="navbar-item active">
                        <a className="navbar-item-link"><span className="fa fa-comment"></span> TRIVIA</a>
                    </div>

关于即使在弹出 create-react-app 后,css 模块也无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48395860/

相关文章:

html - 具有不同高度单元格的自适应网格布局

html - 框从页脚浮出

php - 是否可以检测视口(viewport)宽度并在 jquery 中写入 mysql,然后在不重新加载页面的情况下在 php 中检索宽度

javascript - 如何使用自定义 Hook 访问结构对象以将背景图像添加到 Canvas ?

javascript - 模块解析失败 : Unexpected character

html - 制作下拉菜单

javascript - 删除 Material UI 轮廓文本字段中的内部填充边框

config - 当设置更多参数时如何编写webpack.config.js?

javascript - 遇到 webpack 模块解析问题

reactjs - 我可以使用React Native开发PWA吗