LESS - 受保護的名稱空間



描述

當保護應用於名稱空間時,只有當保護條件返回 true 時,該名稱空間定義的 Mixin 才會被使用。名稱空間保護類似於 Mixin 上的保護。

示例

以下示例演示了在 LESS 檔案中使用受保護的名稱空間

<html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
      <title>Guarded Namespaces</title>
   </head>

   <body>
      <h2>Welcome to Tutorialspoint</h2>
      <p>This will paragraph be displayed red, when (@color = blue) in style.less 
      and when color is other than blue, then this paragraph will be default black.</p>
   </body>
</html>

接下來,建立style.less檔案。

style.less

@import "https://tutorialspoint.tw/less/lib.less";
#namespace when (@color = blue) {
   .mixin() {
      color: red;
   }
}

p {
   #namespace .mixin();
}

以下程式碼將從路徑https://tutorialspoint.tw/less/lib.less匯入lib.less檔案到style.less中:

lib.less

@color: blue;

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

lessc style.less style.css

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

style.css

p {
   color: red;
}

輸出

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

  • 將上述 html 程式碼儲存到less_mixin_guarded_namespaces.html檔案中。

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

LESS Guarded Namespacess
less_mixins.htm
廣告
© . All rights reserved.