如果变量不存在,是否有一种方法可以在LESS中删除属性?

如果变量不存在,是否有一种方法可以在LESS中删除属性?,less,Less,假设我有一个更少的文件像这样 @myVariable: 5px; .myRule { myProperty1: @myVariable; myProperty2: myOtherValue1; } 当我用更少的资源编译它时,我会得到这个结果 .myRule { myProperty1: 5px; myProperty2: myOtherValue1; } 但是,我不想在这个LESS文件中定义@myVariable,而是想从其他地方引用它。我的引用文件可能包含

假设我有一个更少的文件像这样

@myVariable: 5px;

.myRule {
    myProperty1: @myVariable;
    myProperty2: myOtherValue1;
}
当我用更少的资源编译它时,我会得到这个结果

.myRule {
    myProperty1: 5px;
    myProperty2: myOtherValue1;
}
但是,我不想在这个LESS文件中定义@myVariable,而是想从其他地方引用它。我的引用文件可能包含也可能不包含此变量。目前,如果变量丢失,我将得到如下结果

.myRule {
    myProperty1: ;
    myProperty2: myOtherValue1;
}
.myRule {
    myProperty2: myOtherValue1;
}
如果没有提供变量,我的输出是这样的,那么允许我完全删除属性的功能是否会减少

.myRule {
    myProperty1: ;
    myProperty2: myOtherValue1;
}
.myRule {
    myProperty2: myOtherValue1;
}
我浏览了LESS的语言特性,没有找到任何可以做到这一点的东西。也许我错过了什么

这可能是我想象中的语法,但我很确定这是不存在的

.myRule {
    when(exists(@myVariable)) {
        myProperty1: @myVariable;
    }
    myProperty2: myOtherValue1;
}

我觉得你把语法弄得太复杂了。应该是这样的:

.myRule {
    & when (@myVariable = false) {
      myProperty1: @myVariable;
    }
    myProperty2: myOtherValue1;
}
此功能(显然)称为

看起来您可以检查变量是否具有特定的值,但不能检查它是否允许您检查定义的值。 因此,我想另一个文件应该始终将此变量定义为先决条件,但它可以将其设置为“false”(或任何类型的默认值),以指示根本不应使用此属性。

set default and Override 默认输出

.myRule {
  myProperty2: myOtherValue1;
}
.myRule {
  myProperty1: 5px;
  myProperty2: myOtherValue1;
}
覆盖的输出

.myRule {
  myProperty2: myOtherValue1;
}
.myRule {
  myProperty1: 5px;
  myProperty2: myOtherValue1;
}

但无论如何,我都希望我的价值观在那里。这不会删除MyTherValue1吗?谢谢,这很有帮助,但我不认为这是无效的。当
@myVariable
未定义时,我得到一个“错误:
@myVariable
未定义”。当我定义它时,我需要使它显式地等于true。在它工作之前将其转换为某个值,或者对其使用某种类型的isNumber()或isString()函数。我想我要找的是两件事。首先,是某种isDefined()函数。其次,我不希望它创建两个相同的选择器。我希望when()保护在属性级别工作,而不是在选择器级别。也许我要求的比设计的要多。看起来这是不可能的。或者至少,我找不到办法。哇,这有点复杂,但它回答了我的问题。谢谢