【问题标题】:"Unresolved variable" when using className={styles.nameOfClass}使用 className={styles.nameOfClass} 时出现“未解析的变量”
【发布时间】:2016-10-26 02:18:32
【问题描述】:

我正在尝试使用“className”元素更改 ReactJS 中按钮的样式。但是,我在 WebStorm 中收到一条错误消息,指出“未解析的变量 nameOfClass”,当我运行 webpack 并在 localhost 上打开页面时,没有进行样式更改。我尝试了不同的方法来导入 CSS 文件和命名约定,但都无济于事。

React Component 类的文件在这里:

import * as React from "react";
var styles = require('./Roster.css');
export class Roster extends React.Component<{},{}> {

    render() {
        return (
            <div>
                <button className={styles.nameOfClass} type="button" >Players</button>
            </div>
        );
    }
}

Roster.css 文件在这里:

.nameOfClass {
    background-color: #ff0000;
}

webpack.config.js

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

module.exports = {
    entry: "./src/index.tsx",
    output: {
        filename: "./dist/bundle.js",
    },

    // Enable sourcemaps for debugging webpack's output.
    devtool: "source-map",
    externals: ['axios'],
    resolve: {
        // Add '.ts' and '.tsx' as resolvable extensions.
        extensions: ["", ".webpack.js", ".web.js", ".ts", ".tsx", ".js"]
    },

    module: {
        loaders: [
            // All files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'.
            { test: /\.tsx?$/,
              loader: "ts-loader"
            },
            {
                test: /\.css$/,
                loader: ExtractTextPlugin.extract('style-loader', 'css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]'),
            }
        ],

        preLoaders: [
            // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
            { test: /\.js$/, loader: "source-map-loader" }
        ]
    },

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

    // When importing a module whose path matches one of the following, just
    // assume a corresponding global variable exists and use that instead.
    // This is important because it allows us to avoid bundling all of our
    // dependencies, which allows browsers to cache those libraries between builds.
    externals: {
        "react": "React",
        "react-dom": "ReactDOM"
    },
};

【问题讨论】:

  • 你在 webpack 中使用了 CSS 加载器吗?
  • 是的,我编辑了我的帖子以包含 webpack.config.js
  • 它看起来像 Webstorm 中的一个错误。我正在使用 css-modules 并且当我添加类并导入它们时也收到这些警告,但是样式正确呈现并且 eslint 和 stylelint 很好。

标签: css reactjs typescript


【解决方案1】:

您不必要求样式文件。

import * as React from "react";

export class Roster extends React.Component<{},{}> {

    render() {
        return (
            <div>
                <button className="nameOfClass" type="button" >Players</button>
            </div>
        );
    }
}

还有你的 css 文件:

.nameOfClass {
    background-color: #ff0000;
}

【讨论】:

  • 如果我无法调用其他文件,该文件如何知道“nameOfClass”的来源?
  • 我在 index.html(或 .ejs,或任何你使用的文件)中引用了我的 css 文件
【解决方案2】:

应该看起来像:

import * as React from "react";

export class Roster extends React.Component<{},{}> {
    render() {
        return <div>
            <button className="nameOfClass" type="button">Players</button>
        </div>
    }
}

你不能要求 css 文件,只有 typescript/javascript 文件。

css-modules这个东西,还有一些针对react/css的js解决方案,比如RadiumreactCss

【讨论】:

  • 这不是真的。您可以将 css 文件导入到您的 JS 中以像他上面那样使用
  • @erichardson30 我从未使用过它,所以我可能是错的,但据我了解,您需要一些额外的东西才能使用 css-modules 并且 OP 没有显示他使用它的任何暗示。
  • @NitzanTomer,你知道我需要使用什么额外的插件吗?
  • 不。我从来没有使用过这些。
猜你喜欢
  • 2016-01-04
  • 2017-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-25
  • 2018-03-15
  • 1970-01-01
相关资源
最近更新 更多