javascript – 用于反应的CSS模块

我想在登录组件中添加一个css模块.现在,我在src / styles文件夹中有一个style.css样式表,它正在加载全局样式.但是现在,我想自定义登录组件,所以我将一个login.css文件添加到componentes / login中:

.button {
  background-color: black;
}

我的LoginPage.js组件是:

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

class LoginPage extends React.Component {
  render() {
    return (
      <div className="jumbotron col-md-6 col-md-offset-3">
        <div className="container">
          <form name="form">
            <div className="form-group">
              <label htmlFor="username">Username</label>
              <input type="text" name="username" className="form-control" required />
            </div>
            <div className="form-group">
              <label htmlFor="password">Password</label>
              <input type="password" name="password" className="form-control" required />
            </div>
            <div className="form-group">
              <button className="btn btn-primary">Login</button>
            </div>
          </form>
        </div>
      </div>
    );
  }
}

export default LoginPage;

但我的webpack显示以下错误:

src\components\login\LoginPage.js (2/2)
  ✖  2:20  Parse errors in imported module './Login.css': Unexpected token . (1:1)  import/namespace
  ✖  2:20  Parse errors in imported module './Login.css': Unexpected token . (1:1)  import/default
  !  2:20  Parse errors in imported module './Login.css': Unexpected token . (1:1)  import/no-named-as-default
  !  2:20  Parse errors in imported module './Login.css': Unexpected token . (1:1)  import/no-named-as-default-member

这是我的webpack.config.js

import webpack from 'webpack';
import path from 'path';

export default {
  debug: true,
  devtool: 'inline-source-map',
  noInfo: false,
  entry: [
    'eventsource-polyfill', // necessary for hot reloading with IE
    'webpack-hot-middleware/client?reload=true', //note that it reloads the page if hot module reloading fails.
    path.resolve(__dirname, 'src/index')
  ],
  target: 'web',
  output: {
    path: __dirname + '/dist', // Note: Physical files are only output by the production build task `npm run build`.
    publicPath: '/',
    filename: 'bundle.js'
  },
  devServer: {
    contentBase: path.resolve(__dirname, 'src')
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin()
  ],
  module: {
    loaders: [
      {test: /\.js$/, include: path.join(__dirname, 'src'), loaders: ['babel']},
      {test: /(\.css)$/, loaders: ['style', 'css']},
      {test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: 'file'},
      {test: /\.(woff|woff2)$/, loader: 'url?prefix=font/&limit=5000'},
      {test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/octet-stream'},
      {test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=image/svg+xml'}
    ]
  }
};

最后,我尝试时遇到了问题:

 <div className="form-group">
   <button className=`${styles.button}`>Login</button>
 </div>

src/components/login/LoginPage.js: JSX value should be eith
er an expression or a quoted JSX text (19:32)

如何设置css模块以正确反应?

编辑:

经过下面的讨论,我修复了加载类的错误:

<div className="form-group">
  <button className={styles.button}>Login</button>
</div>

现在,加载了css但webpack仍然显示相同的错误和警告.

最佳答案 问题是你试图将实际的CSS导入你的JS而没有先完成转换到
ICSS

您的webpack加载器只需要为CSS模块设置.为此,您需要为本地化的类名添加模块和可选的模板.

将您的CSS加载器更改为:

{test: /(\.css)$/, loader: 'style!css?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]'}

这将告诉webpack在导入之前将CSS预编译到ICSS中,这变成了一个JS对象,告诉它如何将按钮类名称映射到动态本地化类名称字符串LoginPage__button___ab123

然后,您应该在第一次尝试时通过此导入的对象添加classNames,尽管除非添加多个类,否则不需要插值字符串

<button className={styles.button}>Login</button>
点赞