【问题标题】:Pass function or mixin by reference in SASS在 SASS 中通过引用传递函数或 mixin
【发布时间】:2012-12-27 01:32:22
【问题描述】:

有没有办法通过引用SASS中的另一个函数或mixin来传递一个函数或mixin,然后调用引用的函数或mixin?

例如:

@function foo($value) {
    @return $value;
}

@mixin bob($fn: null) {
    a {
        b: $fn(c); // is there a way to call a referenced function here?
    }
}

@include bob(foo); // is there any way I can pass the function "foo" here?

【问题讨论】:

标签: css sass


【解决方案1】:

函数和 mixin 在 Sass 中不是一等,这意味着您不能像使用变量那样将它们作为参数传递。

Sass 3.2 及以上版本

你能得到的最接近的是@content 指令(Sass 3.2+)。

@mixin foo {
    a {
        @content;
    }
}

@include bob {
    b: foo(c); // this replaces `@content` in the foo mixin
}

唯一需要注意的是 @content 看不到你的 mixin 里面有什么。换句话说,如果 c 仅在 bob 混合内定义,它本质上是不存在的,因为它没有被考虑在范围内。

Sass 3.3 及更新版本

从 3.3 开始,您可以使用 call() 函数,但它只能用于函数,不能用于 mixins。这需要传递包含函数名称的字符串作为第一个参数。

@function foo($value) {
    @return $value;
}

@mixin bob($fn: null) {
    a {
        b: call($fn, c);
    }
}

@include bob('foo');

【讨论】:

  • '将字符串传递给 call() 已被弃用,在 Dart Sass 2.0.0 中将是非法的。'你现在应该传递一个由meta.get-function() 返回的函数。
猜你喜欢
  • 2016-12-20
  • 2016-12-11
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 2019-02-12
  • 2015-03-28
  • 1970-01-01
相关资源
最近更新 更多