javascript - webpack 中的 css 配置不检测类样式但检测元素样式

标签 javascript css reactjs webpack

我正在使用 webpack 和 babel 进行 react 。我有如下的 webpack 配置。当我将 CSS 写入 div、ul、li 等元素时,它会在我的 App.js 中检测到,但是当我尝试编写基于类的 CSS 时,它不会在页面上检测到。

webpack.config.js

const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
  entry: {
    main: path.join(__dirname, '..', '/src/index.js'),
  },
  output: {
    path: path.resolve(__dirname, '..', 'dist'),
    filename: '[name].[hash].bundle.js',
    publicPath: '/',
  },
  module: {
    rules: [
      {
        test: /\.(eot|svg|ttf|woff|woff2)$/,
        loader: 'file-loader?name=[name].[ext]',
      },
      {
        test: /\.js$/,
        use: ['babel-loader'],
        exclude: /node_modules/,
      },
      {
        test: /\.s?css$/,
        exclude: /node_modules/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            {
              loader: 'css-loader',
              options: {
                minimize: true,
                modules: true,
                importLoaders: true,
                localIdentName: '[name]__[local]--[hash:base64:5].css',
                sourcemaps: true,
              },
            },
            {
              loader: 'sass-loader',
              options: {
                sourcemaps: true,
              },
            },
          ],
        }),
      },
      {
        test: /\.s?css$/,
        include: /node_modules/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            {
              loader: 'css-loader',
              options: {
                minimize: true,
              },
            },
            {
              loader: 'sass-loader',
              options: {
                sourcemaps: true,
              },
            },
          ],
        }),
      },
    ],
  },
  resolve: {
    extensions: ['.js', '.jsx'],
  },
};

索引.css

.test {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-gap: 1em; 
  list-style: none;
  padding: 12px;
}

应用程序.js

import React from 'react';
import '../index.css';


const App = () => (
  <div>
     <h1>Hello Adeva!!</h1>
     <ul className="test">
        <li>Romans</li>
        <li>Corinthians</li>
        <li>Thessalonians</li>
        <li>Ephesians</li>
        <li>Phillipians</li>
        <li>Colossians</li>
      </ul>
  </div>
)

export default App;

我的react版本是16.8.1,webpack是4.29.3。请指导我,因为这个 webpack 配置问题让我很困惑。

最佳答案

您已通过在 css-loader 配置中添加 modules: true 将 webpack 配置为使用 css 模块。要使用 css 模块,您必须以 javascript 对象的形式访问样式,如下所示:

import React from 'react';
import styles from '../index.css';


const App = () => (
  <div>
     <h1>Hello Adeva!!</h1>
     <ul className={styles.test}>
  </div>
)

关于javascript - webpack 中的 css 配置不检测类样式但检测元素样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54593484/

相关文章:

javascript - 将鼠标悬停在另一个不相关类的子类上时如何更改一个类的 css?

javascript - Angular JS 使用 ng-show 隐藏 Jquery Multiselect 的某些选项

html - 内联对齐 div,同时将内容从上到下保留在页脚中

php - 拉维尔 : Call to a member function index() on null

reactjs - TypeScript Rest Props - 允许任何 Prop 或告诉组件它们将成为哪些 Prop

reactjs - 点击 ReactJS 组件不会在 OpenLayers Overlay 上触发

javascript - JQUERY scrollTo 执行后不会让我滚动

javascript - 如何使用 Materialise 触发选择更改

css - 范围样式在 Firefox 和 Internet Explorer 中不正确

java - 在 Java 中将坐标从 React 转换为 iText