在SASS中:
.DocumentationArticle
$DocumentationArticle-Heading__TopLevel--Selector: null
&-Heading
&__TopLevel
$DocumentationArticle-Heading__TopLevel--Selector: &
&__Section
@debug($DocumentationArticle-Heading__TopLevel--Selector) // OK: ".DocumentationArticle-Heading__TopLevel"
在手写笔中,变量值已更改,此更改仅在已更改的范围内可见:
.DocumentationArticle
DocumentationArticle-Heading__TopLevel--Selector = null
&-Heading
&__TopLevel
DocumentationArticle-Heading__TopLevel--Selector = selector()
&__Section
warn(DocumentationArticle-Heading__TopLevel--Selector) // null!
如何使DocumentationArticle-Heading__TopLevel--Selector
新值在&__Section
范围内可见?
Stylus提供了两个相同的运算符来实现此?=和:=,如此处所记录:
http://learnboost.github.io/stylus/docs/operators.html#conditional-assignment--
为了完整起见,下面的示例显示了两种替代(但等效)语法:
示例1:
// my-html-object.styl
$my-html-object-color ?= blue
$my-html-object
color $my-html-object-color
示例2:
// my-html-object.styl
$my-html-object-color := blue
$my-html-object
color $my-html-object-color
然后在两种情况下,您都可以按预期调整值:
// main.styl
$my-html-object-color = blue
@import('my-html-object')
我在这里找到了这个答案Assume default value for global variable in Stylus stylesheet?