3

如何在支持内容块的情况下为同一个 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“大断点”不接受内容块。

4

1 回答 1

6

在你的mixin中@content使用时你没有传递任何东西。这样做将修复您的编译错误:@include desktop-breakpointlarge-breakpoint

@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

于 2018-04-26T08:50:18.970 回答