css - 无法使用 webpack 设置 React 组件的样式

标签 css reactjs webpack

所以我尝试向我的 React 组件添加一些 css 样式但失败了。 我的 webpack.config.js 看起来像:

var webpack = require('webpack');
var path = require('path');

var BUILD_DIR = path.resolve(__dirname, './build');
var APP_DIR = path.resolve(__dirname, './src/client');

const config = {
   entry: {
     main: APP_DIR + '/index.js'
   },
   output: {
     filename: 'bundle.js',
     path: BUILD_DIR,
   },
   module: {
    rules: [
     {
       test: /\.css$/,
       use: [{
           loader: "style-loader"
       }, {
           loader: "css-loader?modules=true&camelCase=true"
       }]
     },
     {
       test: /\.(jsx|js)?$/,
       use: [{
         loader: "babel-loader",
         options: {
           cacheDirectory: true,
           presets: ['@babel/preset-react']
         }
       }]
     }
    ],

  }
};

module.exports = config;

我的客户端代码文件夹如下所示:

Client
--style
----index.css
--index.js

index.css 看起来像:

body{
    color: #555;
    background: #f85032;
    margin: 10px 30px;
}

在 index.js 中,我正在使用

加载 css 文件
import css from './style/index.css';

然后我做:

npx webpack
npm start

控制台输出中没有错误消息。该网页显示但没有 css 样式。有人可以帮我吗?谢谢!

看来,如果我在 index.html 中做一些内联 css 就可以了?有什么建议为什么会这样吗?谢谢!

最佳答案

更改为 import './style/index.css'; 并查看是否有效

关于css - 无法使用 webpack 设置 React 组件的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49739573/

相关文章:

Javascript 表单验证

html - 我怎样才能让一些元素周围的点消失/变成实线?

html - Bootstrap 列间距

reactjs - MUI 组选择 - 数组的数组

javascript - 将数据传递回异步请求的调用函数

reactjs - 如何使用自定义 webpack 在 React 上设置环境变量

node.js - 在 Docker 容器内调试 webpack-dev-server 应用程序

html - 如何避免重叠?

javascript - 在 webpack 捆绑库上模拟 fbasyncInit 行为

reactjs - 将自定义 Prop 与 RouteComponentProps 组合时缺少位置、历史和匹配属性