LESS - import 选项 Once 关键字
描述
@import (once) 关键字确保文件仅导入一次,并且该文件的任何后续导入语句都将被忽略。这是 @import 语句的默认行为。此行为在 版本 1.4.0 中发布。
示例
以下示例演示了在 LESS 文件中使用 once 关键字 −
<html>
<head>
<link rel = "stylesheet" href = "style.css" type = "text/css" />
<title>Import Options Once</title>
</head>
<body>
<h1>Welcome to Tutorialspoint</h1>
<p class = "para_1">LESS is a CSS pre-processor that enables customizable,
manageable and reusable style sheet for web site.</p>
<p class = "para_2">LESS is a CSS pre-processor that enables customizable,
manageable and reusable style sheet for web site.</p>
</body>
</html>
现在,创建 style.less 文件。
style.less
@import (once) "http://www.tutorialspoint.com/less/once.less";
@import (once) "http://www.tutorialspoint.com/less/once.less"; // 此声明将被忽略
.para_1 {
color: red;
.style;
}
.para_2 {
color: blue;
}
以下代码将从 https://www.tutorialspoint.com/less/once.less 路径将 once.less 文件导入到 style.less 中 −
once.less
.style {
font-family: "Comic Sans MS";
font-size: 20px;
}
您可以使用以下命令将 style.less 编译为 style.css −
lessc style.less style.css
执行上述命令;它将使用以下代码自动创建 style.css 文件 −
style.css
.style {
font-family: "Comic Sans MS";
font-size: 20px;
}
.para_1 {
color: red;
font-family: "Comic Sans MS";
font-size: 20px;
}
.para_2 {
color: blue;
}
输出
按照以下步骤查看上述代码的工作原理 −
将上述 html 代码保存在 import_options_once.html 文件中。
在浏览器中打开此 HTML 文件,将显示以下输出。

less_import_options.html