Css 减:Can';不要在每个页面内使用地图

Css 减:Can';不要在每个页面内使用地图,css,less,Css,Less,我有以下几点: @threshold:{ a:50; b:200; }; @themes:{ a:red; b:blue; }; .mymixin(@name,@color,@thrshld){ //do-something } each(@themes,{ .mymixin(@key,@value,@threshold[@key]); }); 通过运行代码,会发生以下错误: 运行时错误:错误评估函数每个:未找到变量@key 我正在使用v3.9.0 如何在每个函数

我有以下几点:

@threshold:{
  a:50;
  b:200;
};

@themes:{
  a:red;
  b:blue;
};

.mymixin(@name,@color,@thrshld){
  //do-something
}

each(@themes,{
  .mymixin(@key,@value,@threshold[@key]);
});
通过运行代码,会发生以下错误:

运行时错误:错误评估函数
每个
:未找到变量@key

我正在使用v3.9.0


如何在每个函数中使用映射?

您需要使用
@map[$@property]
语法来计算
@map[@property]

.mymixin(@name, @color, @thrshld) {
  .theme-@{name} {
    color: @color;
    width: @thrshld;
  }
}

@threshold: {
  a: 50;
  b: 200;
};

@themes: {
  a: red;
  b: blue;
};

each(@themes, {
  .mymixin(@key, @value, @threshold[$@key])
})

你看。如果我没有弄错的话,
@threshold[@key]
将在
@threshold
中查找名为
@key
的变量(没有)。要获取由'each's'`
@key
变量的值指定的
@threshold
属性值,请使用
@threshold[$@key]
。(也就是说,认识到所有这些
a[b]
a[@b]
a[$b]
a[@@b]
a[$@b]
语句之间的区别是很重要的)。是的,谢谢,但我更喜欢使用
@threshold(@key)
@threshold(@key)
@threshold(@key)
(虽然就我个人而言,我觉得这种特殊的语法在可读性上更令人困惑-太多的语法变化让我无法记住这个小小的单一功能:(.OMG!它根本不起作用!但是你的解决方案起作用了,我正在删除答案,所以你可以回答,我会接受。别麻烦了(反正我懒得写答案)。如果您将这两个选项都添加到A中,将对未来的读者有所帮助(我想这将非常常见,因为
*A[$@b]
@A($@b)
语法都没有明确的文档记录。例如)。