@error

编写带有参数的混合宏函数时,通常需要确保这些参数具有您的API期望的类型和格式。如果不是,则需要通知用户,并且您的mixin /功能需要停止运行。

Sass通过@error编写的规则使此操作变得容易@error <expression>。它打印表达式的值(通常是一个字符串)以及指示如何调用当前mixin或函数的堆栈跟踪。一旦错误被打印出来,Sass就停止编译样式表,并告诉正在运行任何系统的系统都发生了错误。

SCSS  语法

@mixin reflexive-position($property, $value) {
  @if $property != left and $property != right {
    @error "Property #{$property} must be either left or right.";
  }

  $left-value: if($property == right, initial, $value);
  $right-value: if($property == right, $value, initial);

  left: $left-value;
  right: $right-value;
  [dir=rtl] & {
    left: $right-value;
    right: $left-value;
  }
}

.sidebar {
  @include reflexive-position(top, 12px);
  //       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  // Error: Property top must be either left or right.
}

Sass语法

@mixin reflexive-position($property, $value)
  @if $property != left and $property != right
    @error "Property #{$property} must be either left or right."


  $left-value: if($property == right, initial, $value)
  $right-value: if($property == right, $value, initial)

  left: $left-value
  right: $right-value
  [dir=rtl] &
    left: $right-value
    right: $left-value



.sidebar
  @include reflexive-position(top, 12px)
  //       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  // Error: Property top must be either left or right.

错误和堆栈跟踪的确切格式因实现而异,并且还取决于您的构建系统。这是从命令行运行时在Dart Sass中的外观:

Error: "Property top must be either left or right."
  ╷
3 │     @error "Property #{$property} must be either left or right.";
  │     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  ╵
  example.scss 3:5   reflexive-position()
  example.scss 19:3  root stylesheet