变量名称错误“未定义”即使“variables.less”进口的
我今天开始使用 LESS。但这有点奇怪。这段代码不起作用。我收到错误:
! Variable Name Error: @linkColor in a is undefined.
我的引导程序:
@import "variables.less";
@import "normalize.less";
variables.less:
@linkColor: #08c;
@linkColorHover: darken(@linkColor, 15%);
normalize.less:
a {
color: @linkColor;
}
a:visited {
color: @linkColor;
}
a:hover {
color: @linkColorHover;
}
当我制作
@import“变量.less”
在 normalize.less 文件中,一切正常。
感谢您的帮助 :)
I started using LESS today. But it's kinda weird. This code does not work. I get an error:
! Variable Name Error: @linkColor in a is undefined.
My bootstrap:
@import "variables.less";
@import "normalize.less";
variables.less:
@linkColor: #08c;
@linkColorHover: darken(@linkColor, 15%);
normalize.less:
a {
color: @linkColor;
}
a:visited {
color: @linkColor;
}
a:hover {
color: @linkColorHover;
}
When I make an
@import "variables.less"
in the normalize.less file, everything works fine.
Thanks for your help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
这个其他问题最终引导我找到了正确的答案。
如果文件使用 BOM 编码,LESS 编译器似乎会默默地失败。 (对于那些不熟悉该术语的人来说,这是一个字节顺序标记。)这是默认设置在某些编辑器中,例如 Visual Studio。
如果 BOM 位于根文件中,编译器会在 BOM 上抛出错误,但对于 @import-ed 文件似乎会默默失败。
尝试将文件保存为不带 BOM 的 UTF-8,看看是否能解决您的问题。
This other question ultimately led me to the right answer.
It looks like the LESS compiler is silently failing if files are encoded with a BOM. (That's a Byte Order Mark for those not familiar with the term.) This is the default setting in some editors, such as Visual Studio.
The compiler barfs up an error on the BOM if it's in your root file, but seems to fail silently for @import-ed files.
Try saving your files as UTF-8 without a BOM, and see if that solves your problem.
您导入的文件中的错误导入也可能导致此错误。
当使用多层导入和 Node.js 的“lessc”编译器时,我也遇到了这个问题:
我尝试编译原始文件,并收到相同的“未定义变量”行为。我可以看到该变量是在子项中定义的,并且语法看起来正确。
没有显示先前的错误。
问题原来是孩子没有正确导入孙子。即,
而不是:
修复导入孙子的子项使原始项看到子项中定义的变量。
这似乎是 less 中的一个错误 - 即,错误的导入应该显示在“lessc”输出中,所以有一天它可能会被修复。在那之前,我希望这会有所帮助。
This error can also occur via bad imports in the files you're importing.
I also encountered this issue, when using multiple layers of import, and the 'lessc' compiler from Node.js:
I attempted to compile the original file, and received the same 'undefined variable' behavior. I could see the variable was defined in the child and the syntax lookedcorrect.
No prior errors were displayed.
The problem turned out that the child was not importing the grandchild properly. Ie,
rather than:
Fixing the child importing the grandchild made the original see the variables defined in the child.
This seems like a bug in less - ie, the bad import should show up in the 'lessc' output, so one day it will probably be fixed. Until then, I hope this helps.
可能还有另一个可能的根本原因。
这是我原来的 Gruntfile.js:
通配符使 LESS 编译器编译该文件夹下的所有 .less 文件,并将所有结果合并到一个 CSS 中。我在运行
grunt less:compile
时遇到错误:一旦我将
'less/**/*.less'
更改为'less/css.less '
,编译成功。There may be another possible root cause.
Here is my original Gruntfile.js:
The wildcard makes LESS compiler compile all .less files under that folder and merge all results into one CSS. And I got errors running
grunt less:compile
:Once I changed
'less/**/*.less'
into'less/css.less'
, the compilation succeeds.我在使用Winless编译器时遇到了同样的问题。
我导入 master.less 的一些 .less 文件是空的。当我从导入文件列表中删除这些变量时,我的变量被识别了!
I encountered the same problem using Winless compiler.
Some of the .less files i was importing into master.less were empty. when i removed these from the list of imported files my variables were recognized!
为了帮助其他可能遇到此问题的人不希望通过多次导入生成重复的 CSS,您有两个选择。
或者@import-once您需要在每个文件中使用它们的变量/混合文件。
使用
@import-once "filename.less";
来防止重复。升级到 LESS > 1.4.0,当它到达时;来自 less 网站:
“语句@import在1.4.0之前和之后的行为有所不同。它在所有旧版本中充当@import-multiple,在1.4.0之后的所有less.js版本中充当@import-once。”< /code>
To help any others that may come across this not want duplicate CSS generated from multiple imports, you have two options.
Either @import-once the variables / mixins files you need in each file you need to use them in.
Use
@import-once "filename.less";
to prevent duplicates.Upgrade to LESS > 1.4.0, when it arrives; From the less website:
"The statement @import acts differently before and after 1.4.0. It acts as @import-multiple in all older versions and as @import-once in all less.js versions after 1.4.0."
如果您尝试导入文件两次(不是一个好主意),并且第一次导入是在加载 your.less 文件中引用的变量之前,您也可能会收到此错误
注意:我
在 index.html 中 使用 django compress我有:
然后在样式中。更少我有
You can also get this error if you are trying to import the file twice (not a good idea) and the first import is before your variables referenced in your.less file have been loaded
Note: I'm using django compress
in index.html i had:
then in styles.less i had
我认为这是因为您正在编译哪个 master less 文件。同样,
如果您有 Less/Project_name/project.less 并且在此 project.less 中,您将导入变量 less 以及该目录中的所有其他文件。
你只需要把project.less编译到你的css中,而不是所有的less文件。如果您尝试同时编译project.less和variables.less,则会出现此错误。并且可以避免导入变量较少文件的冗余声明
I think it is because of which master less file you are compiling. Likewise
If you have Less/Project_name/project.less and in this project.less you import the variable less and all the other files which has in the directory.
You just have to compile project.less into your css, not all less files. If you try to compile project.less and variables.less at a time you will have this error. And you can avoid redundant declaration of importing the variable less files
我会在 normalize.less 中使用嵌套规则:
在 @import 中,您不需要使用“.less”,它是可选的:
我不知道这是否有帮助......
I would use the nested rules in the normalize.less :
And in the @import, you don't need to use the ".less", it's optional :
I don't know if this can help...
另一种奇怪的特定情况会发生这种情况:如果您使用 .NET 和 dotless,编译器将因带有变量说明符的嵌套媒体查询而阻塞。如果你有这样的代码:
... 那么 dotless 会告诉你它找不到 @anotherVariable 的定义,即使它使用了上面的三行。
One other weirdly specific situation in which this occurs: if you're using .NET and dotless, the compiler will choke on nested media queries with variable specifiers. If you have code like this:
... then dotless will tell you that it can't find the definition for @anotherVariable, even if it's used three lines above.
对我来说,它是在使用 @Import-once 时发生的,没有任何帮助。
但使用@Import 就可以了。
正如我在《Less》的最新版本中读到的那样,导入将作为导入一次运行。
For me its happened when using @Import-once and nothing help.
but with @Import its worked.
as i read in the last version of the Less the Import will work as Import-once.