Skip to content
On this page

内置 API

Stylus 支持内置 API。这允许 mixins 和函数对调用者进行反射。

混合器(Mixin)

局部变量 mixin 会在函数体内自动赋值。 如果函数在顶层调用,它包含字符串 root,或表示其他情况的 block,最后如果是调用函数期望返回值,则为 false

在以下示例中,我们定义了 reset() 来根据是否在顶层混合、混合到另一个块中或混合到返回值中来改变其行为,如下面的 foo 属性所示:

stylus
reset()
  if mixin == 'root'
    got
      root true
  else if mixin
    got 'a mixin'
  else
    'not a mixin'

reset()

body
  reset()
  foo reset()
reset()
  if mixin == 'root'
    got
      root true
  else if mixin
    got 'a mixin'
  else
    'not a mixin'

reset()

body
  reset()
  foo reset()

编译为:

css
got {
  root: true;
}
body {
  foo: "not a mixin";
  got: "a mixin";
}
got {
  root: true;
}
body {
  foo: "not a mixin";
  got: "a mixin";
}

Released under the MIT License.