更少的混合和变量 [英] Less mixin and variables

查看:60
本文介绍了更少的混合和变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下mixin:

.iconFont(@color: @green, @font-size: 18px){
  color: @color;
  font-size: @font-size;
}

如果我只想更改第二个变量值,我需要写第一个变量默认值吗?

If I want only to change the second variable value, I need to write the first variable default value?

h1{
 .iconFont(@green, 14px);
}

推荐答案

否,调用函数时无需为第一个参数指定默认值.相反,您可以只使用命名参数功能来明确让编译器知道您在mixin调用中传递的值是第二个参数.

No, there is no need to specify the default value for the first parameter while calling the function. Instead you can just use named parameters feature to explicitly let the compiler know that the value you are passing in the mixin call is for the 2nd parameter.

.sample{
    .iconFont(@font-size:14px);
}

上面的Less代码在编译时将产生下面的输出. (注意:我已将@green设置为#00ff00.)

The above Less code when compiled would produce the below output. (Note: I had set the @green as #00ff00.)

.sample {
    color: #00ff00;
    font-size: 14px;
}


使用命名参数功能时,即使参数的传递顺序也无关紧要.例如,可以如下调用相同的mixin:


While using the named parameter feature, even the order in which the parameters are passed does not matter. For example, the same mixin can be called as follows:

.sample2{
    .iconFont(@font-size:24px, @color: #070707);
}

它将产生以下内容作为输出.

And it would produce the below as output.

.sample2 {
    color: #070707;
    font-size: 24px;
}

这篇关于更少的混合和变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆