少css避免全球化变量

时间:2019-02-21 15:38:00

标签: css less

我正在寻找避免全局扩散变量的最佳方法。

我使用此配置进行了测试:

_import.less

@test: #FFF;

_import2.less

@test: #000;

无需测试

@import (reference) "_import";

body {
    background: @test;
}

test2.less

@import (reference) "_import2";

div {
    background: @test;
}

无索引

@import "test";
@import "test2";

带有lessc index.less test.css的输出仍然看起来像

body {
  background: #000;
}
div {
  background: #000;
}

但是我要寻找的是:

body {
  background: #FFF;
}
div {
  background: #000;
}

使用较少的2.7或3.9可获得相同的行为。
有人知道解决方法吗?
谢谢

1 个答案:

答案 0 :(得分:2)

您始终可以使用“未命名的名称空间”(即& {}块)来隔离任何内容(包括导入的文件)。

例如:

test.less

@import "_import";

body {
    background: @test;
}

test2.less

@import "_import2";

div {
    background: @test;
}

index.less

& {@import "test";}
& {@import "test2";}

根据您的目标,可以将这些& {}块本身直接移到test文件中。

---

参考:Local Variable Scoping in Import Files