【问题标题】:how do you use sass in create-react-app with typescript?你如何在带有 typescript 的 create-react-app 中使用 sass?
【发布时间】:2020-08-05 04:18:16
【问题描述】:

我使用 create-react-app 和 typescript 创建了一个新项目,并在我的项目中添加了一个 sass 文件(在 create-react-app 文档中它说它支持开箱即用的 sass 文件)但我得到了这个我的 sass 文件出错:

Failed to compile.

./src/homepage.style.sass (./node_modules/css-loader/dist/cjs.js??ref--6-oneOf-5-1!./node_modules/postcss-loader/src??postcss!./node_modules/resolve-url-loader??ref--6-oneOf-5-3!./node_modules/sass-loader/dist/cjs.js??ref--6-oneOf-5-4!./src/homepage.style.sass)
SassError: Invalid CSS after ".homepage {": expected "}", was "{"
        on line 1 of /home/taghi/wt-projects/react-ecommerce/src/homepage.style.sass
>> .homepage { {

   -----------^

这是我的 Sass 文件内容:

.homepage {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 80px;
}

.directory-menu {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.menu-item {
  min-width: 30%;
  height: 240px;
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid black;
  margin: 0 7.5px 15px;

  &:first-child {
    margin-right: 7.5px;
  }

  &:last-child {
    margin-left: 7.5px;
  }

  .content {
    height: 90px;
    padding: 0 25px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    border: 1px solid black;

    .title {
      font-weight: bold;
      margin-bottom: 6px;
      font-size: 22px;
      color: #4a4a4a;
    }

    .subtitle {
      font-weight: lighter;
      font-size: 16px;
    }
  }
}

任何帮助将不胜感激

【问题讨论】:

  • SASS 语法是基于缩进的,它不使用大括号......所以只需将文件更改为 .scss 文件
  • 好电话,这就是问题所在,它现在正在工作。谢谢@TomFinney

标签: reactjs typescript sass create-react-app


【解决方案1】:

Sass 不需要大括号和分号。所以它应该是这样的,

.homepage display: flex flex-direction: column align-items: center padding: 20px 80px

您只需删除当前 CSS 代码中的大括号和分号或 json Formatter 即可将 CSS 转换为 SASS。

【讨论】:

    【解决方案2】:

    不需要"{}",只需要缩进

    .homepage 
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 20px 80px;
    

    【讨论】:

      猜你喜欢
      • 2018-11-08
      • 2019-04-07
      • 2021-01-23
      • 2017-04-23
      • 2020-04-28
      • 1970-01-01
      • 2020-09-30
      • 2018-09-16
      • 2018-09-16
      相关资源
      最近更新 更多