typescript - Gatsby:将 SASS 模块与 Typescript 结合使用

标签 typescript sass gatsby css-modules

我正在尝试让 typescript 和 sass 模块在 Gatsby 项目中协同工作。

我正在使用 gatsby-plugin-ts-loadergatsby-plugin-sass 插件。

index.tsx

import * as React from 'react';
import styles from './index.module.scss'; // TS2307: Cannot find module './index.module.scss'.

export default () => <div  className={styles.root}>
    <h1>Hello Gatsby!</h1>
</div>;

index.module.scss

.root {
  h1 {
    color: red;
  }
}

我收到以下错误 TS2307: Cannot find module './index.module.scss'.。 如果我将 @ts-ignore 放在 scss 导入行之前,我会得到 TypeError: index_module_scss_1.default is undefined

最佳答案

  1. "esModuleInterop": true 添加到 tsconfig.json
  2. 添加一个文件src/global.d.ts,内容为:
// necessary to make scss module work. See https://github.com/gatsbyjs/gatsby/issues/8144#issuecomment-438206866
declare module '*.scss' {
    const content: {[className: string]: string};
    export = content;
}
  1. 添加 import import styles from './index.module.scss';
  2. 如果它编译了,但 css 没有显示,请确保它是正确的,比如混淆 .rootroot ;)。

来源https://github.com/gatsbyjs/gatsby/issues/8144#issuecomment-438206866

关于typescript - Gatsby:将 SASS 模块与 Typescript 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58938547/

相关文章:

reactjs - Uncaught TypeError : Object(. ..) 不是 React、Formik 和 Webpack 的函数

html - CSS 脉动点,里面有文字

css - 将 SCSS `@extends` 与深层元素样式一起使用

reactjs - 如何在 Gatsby 中集成 whatwg-fetch

file - 如何访问 Gatsby 中 "file"或 "allFiles"GraphQL 查询返回的文件的文本内容?

reactjs - 在 Netlify 上调试失败的 Gatsby 构建(无法解析组件)

javascript - TypeScript包含@types而不包含JS库的原理是什么?

angular - Typescript ESDoc 如何衡量评论覆盖率?

typescript - Angular2/Typescript - 获取服务器延迟

css - 如何在 Sass 中同时使用@support 和@media?