且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Sass - 用另一个变量分配变量

更新时间:2023-01-08 15:20:08

来自文档:http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#variable_defaults_

如果变量尚未分配,您可以通过添加来分配给变量!default 标志到值的末尾.这意味着,如果变量已经被赋值,它不会被重新赋值,但是如果它还没有值,它会被赋予一个.

You can assign to variables if they aren’t already assigned by adding the !default flag to the end of the value. This means that if the variable has already been assigned to, it won’t be re-assigned, but if it doesn’t have a value yet, it will be given one.

例如:

$content: "First content";
$content: "Second content?" !default;
$new_content: "First time reference" !default;

#main {
  content: $content;
  new-content: $new_content;
}

被编译为:

#main {
  content: "First content";
  new-content: "First time reference"; }

带有空值的变量被 !default 视为未赋值:

Variables with null values are treated as unassigned by !default:

$content: null;
$content: "Non-null content" !default;

#main {
  content: $content;
}

被编译为:

#main {
  content: "Non-null content"; }