4

一切似乎都很好,没有错误,但页面上的元素仍然没有样式。

webpack.config.js

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


plugins: [
  new ExtractTextPlugin('app.css', {
    allChunks: true
  }),
],

布局.js

import React, { Component } from 'react';
import InteractiveMap from './InteractiveMap';
import Header from './header';
import Navigation from './navigation';
import CSSModules from 'react-css-modules';
import styles from './Layout.css';

class Layout extends Component {
  render() {
    return (
      <div>
        <div className={styles.mapContainer}>
         <InteractiveMap /> 
       </div>
       <div>
         <Header className={styles.header}>
            <Navigation menuItems={menuItems}/>
         </Header>
        </div>
      </div>
    );
  }
}

export default CSSModules(Layout, styles);

布局.css

//testing

.mapContainer: {
    padding: 0;
    background-color: black;
    height: 100%;
    color: yellow;
}

.header {
    color: yellow;
    background-color: blue;
}

编译的app.css

.Layout__mapContainer___3yH5h: {
  padding: 0;
  background-color: black;
  height: 100%;
  color: yellow;
}

.Layout__header___2kJ4F {
    color: yellow;
    background-color: blue;
}

正如您在图片中看到的那样,已生成类并应用于元素,但在页面上没有显示任何内容。 图片

4

3 回答 3

1

在 react-css-modules 中,您使用styleName而不是 className:

<div styleName="container">

这应该有效:

class Layout extends Component {
  render() {
    return (
      <div>
        <div styleName="mapContainer">
         <InteractiveMap /> 
       </div>
       <div>
         <Header styleName="header">
            <Navigation menuItems={menuItems}/>
         </Header>
        </div>
      </div>
    );
  }
}
于 2016-09-16T06:18:12.483 回答
0

我遇到了同样的问题..我的问题是我没有在我的 .html 文件中包含生成的 css 样式表。

没有样式表

<html>
<head>
</head>

<body>
    <div id = "hero"> </div>
    <script type="text/javascript" src="bundle.js"></script>
</body>

使用样式表(修复)

<html>
<head>
     <link rel='stylesheet' href='app.css'>
</head>

<body>
    <div id = "hero"> </div>
    <script type="text/javascript" src="bundle.js"></script>
</body>

于 2018-07-22T22:17:45.300 回答
0

您需要添加前缀“模块”,例如:

import styles from './Layout.module.css';
于 2021-01-11T09:41:16.260 回答