【问题标题】:Load CSS into React Component not working, using webpack使用 webpack 将 CSS 加载到 React 组件中不起作用
【发布时间】:2021-11-08 22:39:06
【问题描述】:

所以我启动了我的应用程序并尝试将 CSS 用于我的 React 组件,我将我的 css 规则包含在 webpack 文件中:

这是文件结构:

我得到的错误是:

ERROR in ./client/src/related_items/styles/Card.css 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> .Card-Component {
|   border: 1px solid black;
| }
 @ ./client/src/related_items/Card.jsx 2:0-27
 @ ./client/src/related_items/App.jsx 2:0-26 8:163-167
 @ ./client/src/App.jsx 26:0-51 78:43-55
 @ ./client/src/index.js 3:0-28 4:50-53

webpack 5.52.0 compiled with 1 error in 13 ms

webpack.config.js

const path = require('path');

const SRC_DIR = path.join(__dirname, 'client', 'src');
const OUT_DIR = path.join(__dirname, 'client', 'dist');

module.exports = {
  mode: 'development',
  entry: path.join(SRC_DIR, 'index.js'),
  output: {
    path: OUT_DIR,
    filename: 'bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: 'babel-loader'
      },
      {
        test: /\.css$/,
        use: [
          'style-loader',
          'css-loader'
        ]
      },
    ]
  },
  resolve: {
    extensions: ['.js', '.jsx']
  },
};

我只是想在组件中使用 CSS:

卡片组件

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

const Card = () => {
  return (
    <div className="Card-Component">
      <h1>This is a Card</h1>
    </div>
  )
}

export default Card;

这是我的 CSS:

.Card-Component {
  border: 1px solid black;
}

这是我的 package.json

{
  "name": "front-end-capstone",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "react-dev": "webpack -w",
    "server-dev": "nodemon server/index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.15.5",
    "@babel/preset-env": "^7.15.4",
    "@babel/preset-react": "^7.14.5",
    "axios": "^0.21.4",
    "babel-loader": "^8.2.2",
    "babel-preset-es2015": "^6.24.1",
    "chai": "^4.3.4",
    "css-loader": "^6.2.0",
    "express": "^4.17.1",
    "http-proxy-middleware": "^2.0.1",
    "jest": "^27.1.1",
    "mocha": "^9.1.1",
    "nodemon": "^2.0.12",
    "path": "^0.12.7",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "sass": "^1.39.0",
    "sass-loader": "^12.1.0",
    "style-loader": "^3.2.1",
    "webpack": "^5.52.0",
    "webpack-cli": "^4.8.0"
  }
}

【问题讨论】:

  • 你能给我看一下文件结构的截图吗?您可能错误地导入了 CSS
  • @TomasMota 我在顶部包含了文件结构
  • @TomasMota 问题已修复,我杀死了我的 webpack 并在监视模式下重新运行它,由于某种原因,它没有注册更改
  • 我很高兴它成功了。但是,如果您在 Card.jsx 中并且想要访问样式中的 Card.css,则需要添加额外的 .在源 ``` ../styles/card.css

标签: css reactjs webpack


【解决方案1】:

刚刚杀死了webpack并重新运行它,以为进入监视模式就可以了

【讨论】:

    猜你喜欢
    • 2018-06-25
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 2021-08-03
    相关资源
    最近更新 更多