目录

受保护的命名空间(Guarded Namespaces)

描述 (Description)

当guard应用于名称空间时,仅当保护条件返回true时才使用由名称空间定义的mixin。 namespace guard与mixins上的namespace guard类似。

例子 (Example)

以下示例演示了在LESS文件中使用受guarded namespaces -

<html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
      <title>Guarded Namespaces</title>
   </head>
   <body>
      <h2>Welcome to IoWiki</h2>
      <p>This will paragraph be displayed <i>red</i>, when <i>(@color = blue)</i> in <i>style.less</i> 
      and when color is other than <i>blue</i>, then this paragraph will be default <i>black</i>.</p>
   </body>
</html>

接下来,创建style.less文件。

style.less

@import "http://www.iowiki.com/less/lib.less";
#namespace when (@color = blue) {
   .mixin() {
      color: red;
   }
}
p {
   #namespace .mixin();
}

以下代码将从路径https://www.iowiki.com/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;
}

输出 (Output)

请按照以下步骤查看上述代码的工作原理 -

  • 将上述html代码保存在less_mixin_guarded_namespaces.html文件中。

  • 在浏览器中打开此HTML文件,将显示以下输出。

LESS Guarded Namespacess
↑回到顶部↑
WIKI教程 @2018