LESS - 匯入選項 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) "https://tutorialspoint.tw/less/once.less";
@import (once) "https://tutorialspoint.tw/less/once.less"; // this statement will be ignored
.para_1 {
   color: red;
   .style;
}

.para_2 {
   color: blue;
}

以下程式碼將從 https://tutorialspoint.tw/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 Once
less_import_options.htm
廣告

© . All rights reserved.