在另一个`.scss`文件中包含`.scss`文件?

osh*_*rdo 22 sass scss-lint

我怎样才能将.scss文件包含在另一个.scss文件中?我试图在一个文件中写这个:app.scss:

@include('buttons');
@include('dropzone');

body {

    background: $primaryColor;
    overflow-x: hidden; /*Clip the left/right edges of the content inside the <div> element - if it overflows the element's content area: */
    height: 100%; /* Cover all (100%) of the container for the body with its content */
    padding-top: 70px;
} /* more css code here */
Run Code Online (Sandbox Code Playgroud)

它会返回一个错误: invalid css after @import

我尝试在主scss文件中包含2个其他scss 文件,因此它css最终将被编译为一个文件.这怎么可能?

Mer*_*ken 20

你可以像这样导入它;

@import "../../_variables";

@import "../_mixins";

@import "_main";

@import "_login";

@import "_exception";

@import "_utils";

@import "_dashboard";

@import "_landing";
Run Code Online (Sandbox Code Playgroud)

根据你的目录,它会做你想要的.

  • 我做到了。并且用 gulp 编译不会抛出任何错误。问题是样式根本没有应用于页面。 (2认同)
  • 您需要将您的 sass 文件夹预编译到 css 文件夹,然后将您编译的 css 包含到您的页面中。 (2认同)

Ano*_*ngh 10

您可以使用@use规则。此规则将另一个 Sass 文件作为模块加载,这意味着您可以使用基于文件名的命名空间来引用 Sass 文件中的变量、混合和函数。使用文件还将在编译输出中包含它生成的 CSS!

// _base.scss
$font-stack:    Helvetica, sans-serif;
$primary-color: #333;

body {
  font: 100% $font-stack;
  color: $primary-color;
}
Run Code Online (Sandbox Code Playgroud)

看看如何使用@use 'base';在styles.scss文件中

// styles.scss
@use 'base';

.inverse {
  background-color: base.$primary-color;
  color: white;
}
Run Code Online (Sandbox Code Playgroud)

您不需要包含文件扩展名。

  • @osherdo 请将此作为有效答案。由于“@import”支持,大多数排名答案都已过时。Sass 团队**不鼓励继续使用 @import 规则**。Sass 将在未来几年内逐步逐步淘汰它,并最终从语言中完全删除它......[来源](https://sass-lang.com/documentation/at-rules/import) (2认同)

Wim*_*ens 6

您可以通过执行以下操作来包含部分:

@import "partial";
Run Code Online (Sandbox Code Playgroud)

导入的文件需要一个下划线,因此sass会识别它包含在内:_partial.scss