替代解决方法(针对特定用例):
https://sass-lang.com/documentation/at-rules/mixin#passing-arbitrary-arguments
有趣的事实:因为参数列表同时跟踪位置参数和关键字参数,所以您可以使用它同时将两者传递给另一个 mixin。这使得为 mixin 定义别名变得非常容易!
如果您对 mixin 插值感兴趣,因为您有一组 mixin,如下所示:
//_mixins.scss
@mixin text-style-1($args...){ //sass here }
@mixin text-style-2($args...){ //sass here }
@mixin text-style-3($args...){ //sass here }
//_text.scss
.text-style-1 {
@include text-style-1;
}
.text-style-1-contrast {
@include text-style-1($contrast: true);
}
.text-style-2 {
@include text-style-2;
}
.text-style-2-contrast {
@include text-style-2($contrast: true);
}
我们可以利用传递任意参数并为组使用别名:
//_mixins.scss
@mixin text-style-1($args...){ //sass here }
@mixin text-style-2($args...){ //sass here }
@mixin text-style-3($args...){ //sass here }
@mixin text($mixin, $args...) {
@if $mixin == 'style-1' { @include text-style-1($args...); }
@else if $mixin == 'style-2' { @include text-style-2($args...); }
@else if $mixin == 'style-3' { @include text-style-3($args...); }
}
//_text.scss
$text-styles: 'style-1', 'style-2', 'style-3';
@each $style in $text-styles {
.text-#{$style} {
@include text($style);
}
.text-#{$style}-contrast {
@include text($style, $contrast: true);
}
}