SASS How to set a Variable to equal nothing?

2019-02-02 21:10发布

I am trying to create a variable that is equal to nothing. I have tried the following..

$false:null;

-and-

$false:" ";

What I am trying to use it with is this...

@mixin myMixin($myVariable:"", $myOtherVariable){
    $false:null;
    @if $myVariable == $false {
       //do something
    }
    @if $myVariable != "" {
      //do something else
    }
}

@include myMixin("", $myOtherVariable);

I am using two variables for this example, but I plan to have more than two.

Basically what I want to do is have the option to exclude a variable without having to use empty quotes. And the problem with $false: ""; Is that it still expects empty quotes. So I am wondering if there is something built into SASS to designate "null / or nothing". I thought it might be null. But that doesn't seem to be the case..

标签: sass
3条回答
放我归山
2楼-- · 2019-02-02 21:22

Set the variable to 'false'

$false: false;

查看更多
该账号已被封号
3楼-- · 2019-02-02 21:31

Sass is written in ruby which uses nil instead of null.

@mixin myMixin($myVariable, $myOtherVariable){
  $false: nil;
  @if $myVariable == $false{
     color: inherit;
  } @else {
     color: $myVariable;
  }
}

p {
  @include myMixin(nil, $myOtherVariable);
}

.red {
  @include myMixin(red, $myOtherVariable);
}

Example on jsFiddle

查看更多
聊天终结者
4楼-- · 2019-02-02 21:41

null or false will work (null is new in the latest version of Sass). Both will work for your example. The only advantage of null is that it disappears if you use it with a property.

@mixin myMixin($myVariable: false, $myOtherVariable: false){
  @if not $myVariable {
     //do something
  } @else {
    //do something else
  }
}

@include myMixin(false, $myOtherVariable);
查看更多
登录 后发表回答