當(dāng)guard應(yīng)用于命名空間時(shí),只有在guard條件返回true時(shí)才使用由命名空間定義的mixin。 命名空間防護(hù)類似于mixins上的guard。
下面的示例演示了在LESS文件中使用防護(hù)名稱空間:
<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>
接下來,創(chuàng)建文件 style.less 。
@import "//www.15014759268.cn/less/lib.less"; #namespace when (@color = blue) { .mixin() { color: red; } } p{ #namespace .mixin(); }
將路徑從http://www.w3cshool.cn/statics/demosource/lib.less導(dǎo)入到style.less中的lib.less文件
@color: blue;
您可以使用以下命令將 style.less 編譯為 style.css :
lessc style.less style.css
接下來執(zhí)行上面的命令,它將用下面的代碼自動(dòng)創(chuàng)建 style.css 文件:
p { color: red; }
讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:
在 less_mixin_guarded_namespaces.html 文件中保存html代碼。
在瀏覽器中打開此HTML文件,將顯示如下輸出。
更多建議: