如何创建支持内容块的 Sass mixin 别名?

How to create Sass mixin aliases with support for content blocks?

如何在支持内容块的情况下为同一个 mixin 定义多个名称?

定义

@mixin desktop-breakpoint {
   @media only screen and (min-width: 769px) {
      @content;
   }
}

@mixin large-breakpoint {
   @include desktop-breakpoint;
}

用法

.my-class {
   font-size: small;

   @include desktop-breakpoint {
      font-size: big;
   }
}

.my-other-class {
   color: red;

   @include large-breakpoint {
      color: blue;
   }
}

错误信息

Mixin "large-breakpoint" does not accept a content block.

large-breakpoint 混入中使用 @include desktop-breakpoint 时,您没有传递任何 @content。这样做将修复您的编译错误:

@mixin large-breakpoint {
   // Remember to pass content received by mixin to @include
   @include desktop-breakpoint {
     @content;
   }
}

然后你的 CSS 将按预期正确编译:

.my-class {
  font-size: small;
}
@media only screen and (min-width: 769px) {
  .my-class {
    font-size: big;
  }
}

.my-other-class {
  color: red;
}
@media only screen and (min-width: 769px) {
  .my-other-class {
    color: blue;
  }
}

查看基于您修改后的代码的概念验证示例:https://www.sassmeister.com/gist/3109af060293eed0b89a22c27fa20527