简体   繁体   English

无法将 CSS 模块导入 React TypeScript 项目 #121

[英]Unable to import CSS modules into React TypeScript project #121

Describe the bug In my React Typescript project, I am trying to use CSS modules.描述错误在我的 React Typescript 项目中,我正在尝试使用 CSS 模块。 I created the project using create-react-app , added TypeScript later.我使用create-react-app创建了项目,后来添加了 TypeScript。 Then I followed the instructions from the docs to setup CSS modules in the project然后我按照文档中的说明在项目中设置 CSS 模块

  1. Added the plugin with npm install -D typescript-plugin-css-modules添加了带有npm install -D typescript-plugin-css-modules
  2. Then updated tsconfig.json然后更新了 tsconfig.json
{
  "compilerOptions": {
    "plugins": [{ "name": "typescript-plugin-css-modules" }]
  }
}
  1. I tried to run it but it didn't run.我试图运行它,但它没有运行。 It complained about import statement here .在这里抱怨 import 声明。 Though the plugin docs say it shouldn't虽然插件文档说它不应该
  2. So I added global.d.ts , which resolved the error所以我添加了global.d.ts ,它解决了错误
  3. Now when I run it, the Home link on the header should be white.现在当我运行它时,header 上的 Home 链接应该是白色的。 But I see the default color但我看到默认颜色

To Reproduce重现

  1. Go to https://codesandbox.io/s/summer-haze-ztnf6?file=/src/index.tsx Go 到https://codesandbox.io/s/summer-haze-ztnf6?file=/src/index.tsx
  2. See the Link Home查看链接主页

Expected behavior Home link color should be white预期行为主页链接颜色应为白色

Since you already solved the issue, please have a look for description: problem in accessing the scss variables in react components由于您已经解决了该问题,请查看描述: Problem in access the scss variables in react components

in a similar way you can access classes from the module scss files.以类似的方式,您可以从模块 scss 文件中访问类。

Never mind, changing the name of the scss file to header.module.scss fixed the issue.没关系,将 scss 文件的名称更改为header.module.scss解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM