Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何修复css文件上的typescript编译器错误?_Reactjs_Typescript_Webpack_Css Modules - Fatal编程技术网

Reactjs 如何修复css文件上的typescript编译器错误?

Reactjs 如何修复css文件上的typescript编译器错误?,reactjs,typescript,webpack,css-modules,Reactjs,Typescript,Webpack,Css Modules,我正在尝试使用webpack创建一个带有typescript的React项目。但我无法让CSS模块正常工作。在尝试导入typescript中的CSS文件时,我不断遇到此错误: ERROR in src/components/Button.module.css:1:0 [unknown]: Parsing error: Declaration or statement expected. > 1 | .myButton { 2 | box-shadow: 0px

我正在尝试使用webpack创建一个带有typescript的React项目。但我无法让CSS模块正常工作。在尝试导入typescript中的CSS文件时,我不断遇到此错误:

ERROR in src/components/Button.module.css:1:0
[unknown]: Parsing error: Declaration or statement expected.
  > 1 | .myButton {
    2 |         box-shadow: 0px 0px 0px -1px #1c1b18;
    3 |         background:linear-gradient(to bottom, #eae0c2 5%, #ccc2a6 100%);
    4 |         background-color:#eae0c2;
ℹ 「wdm」: Failed to compile.
对我来说,这看起来像是webpack期望css文件包含有效的typescript,但我不确定。我试着四处看看,虽然很多人似乎都在绞尽脑汁地使用typescript和CSS模块,但我找不到有类似问题的人

我像这样导入了CSS文件: Button.tsx

import React from "react";
import "./Button.module.css";

export class Button extends React.Component{
...
}
这就是我要导入的CSS文件。 Button.module.css:

.myButton {
    box-shadow: 0px 0px 0px -1px #1c1b18;
    background:linear-gradient(to bottom, #eae0c2 5%, #ccc2a6 100%);
    background-color:#eae0c2;
    border-radius:22px;
    border:4px solid #706752;
    display:inline-block;
    cursor:pointer;
    color:#505739;
    font-family:Arial;
    font-size:16px;
    font-weight:bold;
    padding:11px 22px;
    text-decoration:none;
    text-shadow:0px 1px 0px #ffffff;
}
.myButton:hover {
    background:linear-gradient(to bottom, #ccc2a6 5%, #eae0c2 100%);
    background-color:#ccc2a6;
}
.myButton:active {
    position:relative;
    top:1px;
}
生成的Button.module.css.d.ts

// This file is automatically generated.
// Please do not change this file!
interface CssExports {

}
export const cssExports: CssExports;
export default cssExports;
我还有一个css模块的类型声明,名为styles.d.ts

declare module "*.module.css" {
    const classes: { [key: string]: string };
    export default classes;
}
虽然Webpack似乎确实为css模块生成了一些打字,但在我看来,它看起来奇怪地是空的。我想我在css模块typescript加载器上做了一些错误,我尝试了一些可用的插件,但仍然无规则地遇到同样的错误

以下是我在webpack.config.ts中配置的内容:

import webpack from "webpack";

const config: webpack.Configuration = {
  entry: "./src/index.tsx",
  resolve: {
    extensions: [".tsx", ".ts", ".js", ".css"],
  },
  module: {
    rules: [
      {
        test: /\.module.css$/,
        use: [
          "css-modules-typescript-loader",
          {
            loader: 'css-loader',
            options: {
              modules: true,
              sourceMap: true,
            }
          },
        ]
      },
      {
        test: /\.(ts|js)x?$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: [
              "@babel/preset-env",
              "@babel/preset-react",
              "@babel/preset-typescript",
            ],
          },
        },
      },
    ],
  },
tsconfig.json

{
  "compilerOptions": {
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react",
    "target": "ES5",
    "plugins": [
      {
        "name": "typescript-plugin-css-modules"
      }
    ]
  },
  "include": [
    "src"
  ],
}

我也有同样的问题。就我而言,这不是网页的问题。这是打字脚本eslint的问题。我通过在.eslintignore文件中添加“*.css”解决了这个问题