为什么我无法将 .less 文件导入到单个 .less 文件中?

发布于 2024-10-27 21:59:55 字数 840 浏览 1 评论 0原文

我的文件结构如下:

lib/css/... 包含我的样式,这些样式分为每种区域的单个 .less 文件。

lib/style.less 是我想要将子样式文件收集到的文件 - 也是我想要链接到 HTML 中的文件。

当我输入(到 style.less 中)时:

@import url("/css/StyleToImport.less");

@import "/css/StyleToImport .less";

...我收到语法错误。

真的不可能将 .less 文件合并为一个文件吗?

使用 1 个文件来包含颜色、尺寸等的所有变量可能非常方便。

但现在,我必须在 HTML 中对每个文件使用 标签单个文件 - 这不太方便。

PS我已阅读将两个 .less 文件加入一个 css 文件

我读过这个:

导入

导入效果很好 正如预期的那样。您可以导入 .less 文件,其中的所有变量都会 可用。如果文件是 .less, 扩展名是可选的:

@导入“库”;

@import“typo.css”;

I have this structure of my files:

lib/css/... contains my styles divided into single .less files for each kind of area.

lib/style.less is the file I want to gather my sub-style files into - and the file I want to be linked into the HTML.

When I type in (into style.less):

@import url("/css/StyleToImport.less");

or

@import "/css/StyleToImport.less";

... I get a syntax error.

Is it really impossible to combine .less files into a single file?

It could be really handy to have 1 single file to contain all variables for colors, dimensions etc.

But as it is now, I have to use <link ...> tags in HTML to every single file - which is not so handy.

P.S. I have read Join two .less files into one css file

and I have read this:

Importing

Importing works pretty much
as expected. You can import a .less
file, and all the variables in it will
be available. If the file is a .less,
the extension is optional:

@import "library";

@import "typo.css";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自由如风 2024-11-03 21:59:55

我刚刚在本地环境中尝试过此操作,将 vars 放入 vars.less 中,并将 mixins 放入 conf.less 中。文件结构与您的类似,基本 .less 文件位于包含所有“包含”的文件夹下方。

所以我有 css 文件夹,其中有我的 main.less 文件,然后是 css/less/vars.less | 要导入

这两个内容:

@import "less/vars.less";
@import "less/conf.less";

我唯一的答案是删除 @import 语句中的第一个 / 。当我尝试将 google 字体导入我的 .less 文件时,我遇到了类似的问题。

至少值得一试,因为使用与你我的结构相同的结构是有效的。

I just tried this on my local environment, putting vars into vars.less and mixins into conf.less. The file structure similar to yours, with the base .less file a level below the folder containing all the 'includes'.

So I had css folder which had my main.less file and then css/less/vars.less | conf.less

To import both of those:

@import "less/vars.less";
@import "less/conf.less";

My only answer is to remove the first / in your @import statement. I had similar problems when trying to import a google font into my .less file.

Worth a shot at least, because using the same structure as yours mine is working.

醉梦枕江山 2024-11-03 21:59:55

我遇到了类似的问题,导入似乎默默地失败了。事实证明,我的编辑器已将 @import-ed 文件保存为带 BOM 的 UTF-8,如 这个问题

无论出于何种原因,LESS 编译器都会在具有 BOM 的 @import-ed 文件上默默地阻塞,即使如果您的 root 文件具有 BOM,它也会抛出错误。

尝试重新保存没有 BOM 的 @import-ed 文件,它可能会起作用。

I had a similar problem where the imports seemed to fail silently. It turned out that my editor had saved the @import-ed files as UTF-8 with BOM, as described in this question.

For whatever reason, the LESS compiler chokes silently on @import-ed files that have a BOM, even though it barfs up an error if your root file has a BOM.

Try re-saving your @import-ed files without a BOM, and it just might work.

世态炎凉 2024-11-03 21:59:55

如果没有错误,我不确定问题是什么。

我建议使用 lessapp,http://incident57.com/less/,我发现它效果很好并且使用起来更容易一些。还要小心尝试导入空白文件,因为它可能会引发错误。

我上传了一些基本代码,用于根据项目构建我的文件 - 您可以在 https://github 获取它们.com/danethurber/seamLESS。这也许能够提供帮助,如果您或任何人想要做出贡献,我会很高兴提供帮助:)

I'm not exactly sure what the problem is without the error.

I would suggest using lessapp, http://incident57.com/less/, which i have found works very well and is a little easier to use. Also be wary of trying to import blank files cause it can throw an error.

I uploaded some basic code that i use to structure my files depending on the project - you can get them at https://github.com/danethurber/seamLESS. That maybe able to help and if you or anybody wants to contribute i'ld be happy for the help :)

甜味拾荒者 2024-11-03 21:59:55

当您链接到 /directory 时,它会从根目录自上而下查找,在这种情况下将找不到它。所以@Daniel 建议删除斜杠是正确的。

您应该这样链接它:

@import url("css/StyleToImport.less");

这样,因为您的起点位于 css 目录本身内,然后向下指向该目录 css/ ,然后指向文件本身。

如果您需要先向上移动一个目录,然后再向下移动,您会这样做

../directory/file.less

As you are linking to /directory it looks from root top down and in this case would not find it. So @Daniel is correct by suggesting to remove the slash.

You should link it as such:

@import url("css/StyleToImport.less");

This, because your starting point is within the css directory itself and then point down into that directory css/ and then pointing to the file itself.

If you would need to go first up a directory and then over and down you would do

../directory/file.less
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文