在 LessCSS 中,如何将 true 分配给变量?
我是 Less 新手,我正在尝试实现简单的 mixin,但这会引发编译错误:
@show-image: true;
.testimg() when not (@show-image) {
display: none;
}
img.test {
.testimg;
}
编译器似乎不喜欢我的 @show-image: true
赋值。我尝试分配数字 1
,但结果是
img.test {
display: none;
}
,这是有道理的(根据 文档)除关键字true
之外的任何值都是假的。
但是我可以将关键字 true
分配给我的 @show-image
变量吗?还是它只适用于参数?
I'm new to Less, and I'm trying to implement simple mixins, but this throws a compilation error:
@show-image: true;
.testimg() when not (@show-image) {
display: none;
}
img.test {
.testimg;
}
It seems the compiler doesn't like my @show-image: true
assignation. I tried assigning the number 1
, but the result was
img.test {
display: none;
}
And that makes sense since (according to the documentation) any value other than the keyword true
is falsy.
But can I assign the keyword true
to my @show-image
variable, or does it only work for with arguments?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Lucho,我同意你的观点......它还很年轻,编译器应该更具描述性,但这并不像你必须定义每个可能的条件(尽管在这种情况下,真/假变量似乎是首先的情况)...只需添加一个失败的“默认”情况,例如“switch”语句:
Lucho, I'm agree with you... it is young and compiler should be a LOT more descriptive, but it is not like you have to define every possible condition (although in this case of a true/false variable it appears to be the case at first)... just have to add a "default" case where fall through, like a "switch" statement:
知道了!
失败的不是分配。看来你只需要为
每个可能的条件定义一个混合默认条件(如@pixshatterer指出< /a>)。所以就我而言,我只需要这样做:
这让我需要指出关于 Less 的两件事。第一个是关于编译器消息,它们应该更具描述性;第二个是关于语言本身的,我认为不自己定义默认情况仍然太愚蠢了。
不要误会我的意思,我认为 Less 很好,而且它非常适合我当前的需求,我只是说它还很年轻,让你重新考虑将它用于大型、重要的项目。
Got it!
It wasn't the assignation what was failing. It seems you just need to define a mixin for
each possible conditionthe default condition (as @pixshatterer pointed out).So in my case, I just have to do this:
This gives me two things to point out about Less. First one is about the compiler messages, they should be more descriptive; the second one is about the language itself, I think it's still too dumb to not define default cases on its own.
Don't get me wrong, I think Less is good, and it's perfect for my current needs, I'm just saying it's still to young and make you reconsider using it for big, important projects.
看起来你没有正确使用它。您需要将
@show-image
传递到函数.testimg()
中,因此结果应该类似于:我尝试使用 dotless 并且它一直在 mixin 定义的右括号上给出解析错误。看起来是时候进行错误报告了。
It looks like you are not using it quite right. You need to pass
@show-image
into the function.testimg()
, so the result should look something like:I tried to test this using dotless and it just kept giving a parse error on the closing parenthesis of the mixin definition. It looks like it's time for a bug report there.