LESS - 匯入選項可選關鍵字



描述

可選關鍵字允許您在檔案不存在時匯入檔案。如果要匯入的檔案不存在且未使用可選關鍵字,則 LESS 會丟擲FileError錯誤並停止編譯。此功能在2.3.0 版中釋出。

示例

以下示例演示了在 LESS 檔案中使用可選關鍵字的方法:

<html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
      <title>Import Options Optional</title>
   </head>

   <body>
      <h1>Welcome to Tutorialspoint</h1>
      <p>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 (optional) "fileNotExist.css";
p {
   color: red;
}

您可以使用以下命令將style.less編譯為style.css

lessc style.less style.css

執行上述命令;它將自動建立style.css檔案,其中包含以下程式碼:

style.css

@import "fileNotExist.css";
p {
   color: red;
}

輸出

請按照以下步驟檢視上述程式碼的工作原理:

  • 將上述 html 程式碼儲存在import_options_optional.html檔案中。

  • 在瀏覽器中開啟此 HTML 檔案,將顯示以下輸出。

LESS Import Options Optional

即使fileNotExist.css檔案不存在,LESS 也會繼續編譯並自動建立 style.css 檔案。

less_import_options.htm
廣告

© . All rights reserved.