SASS 中的条件 mixin 抛出错误

Conditional in SASS mixin throws error

我正在尝试使用条件创建混合,但在编译时遇到错误。生成的错误是:

Error: Properties are only allowed within rules, directives, mixin includes, or other properties.

这是我正在使用的代码:

@mixin mypadder ($topBottomBoth: myDefaultOption, $topUnits: 0, $bottomUnits: 0) {
    @if $topBottomBoth == tp {
        padding-top: 1em;
        }
    @else if $topBottomBoth == bt {
        padding-bottom: 1em;
        }
    @else {
        padding-top: 1em;
        padding-bottom: 1em;
        }
    }

我也试过将 $topBottomBoth 参数的值放在引号中,首先是它出现在参数中的位置(mixin 的第一行),然后另外在 [=19= 的值周围加上引号] 在条件测试中。在每种情况下,我都会遇到相同的错误。看了很多条件混合的例子,我仍然看不出我的语法哪里错了。

你的mixin没有问题,只是你调用错了。 Sass 知道 CSS 属性在选择器之外是不允许的。

不正确:

@include mypadder;

正确:

.foo {
    @include mypadder;
}