在 if 语句内声明的变量会导致“未定义的变量”
我希望在 if 语句中定义变量能够在 Sass 中工作,但不幸的是我收到错误消息,指出该变量未定义。这是我尝试过的:
@for !i from 1 through 9
!foo = #000
@if !i == 1
!bg_color = #009832
@if !i == 2
!bg_color = #195889
...
#bar#{!i}
color: #{!foo}
background-color: #{!bg_color}
使用此代码,我会收到以下错误:
未定义的变量:“!bg_color”。
I was hoping that defining variables in an if statement would work in Sass but unfortunately I get errors saying that the variable isn't defined. Here is what I tried:
@for !i from 1 through 9
!foo = #000
@if !i == 1
!bg_color = #009832
@if !i == 2
!bg_color = #195889
...
#bar#{!i}
color: #{!foo}
background-color: #{!bg_color}
With this code, I would get the following error:
Undefined variable: "!bg_color".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Sass 变量仅对声明它们的缩进级别及其嵌套的缩进级别可见。所以你只需要在 for 循环之外声明 !bg_color:
你会得到以下 css:
Sass variables are only visible to the level of indentation at which they are declared and those nested underneath it. So you only need to declare !bg_color outside of your for loop:
And you'll get the following css: