【问题标题】:SASS variable assignment inside mixin ignored忽略 mixin 中的 SASS 变量赋值
【发布时间】:2019-03-12 06:53:51
【问题描述】:

我想制作一个动态的 SASS 主题切换器,所以我在research 之后做了这个:

$valueName: ("ui_main");
$valueLight: (yellow);
$valueDark: (grey);

$currentTheme: "";

@mixin theme() {

  $currentTheme: "light";
  .light & {
    @content
  }

  $currentTheme: "dark";
  .dark & {
    @content
  }

}

@function getValue($name) {

  $index: index($valueName, $name);

  @if ($index == false) {
    @error $valueName + " has not been defined";
  }

  @if ($currentTheme == "light") {

    @return nth($valueLight, $index);

  } @else if ($currentTheme == "dark") {

    @return nth($valueLight, $index);

  } @else {
    @error "Invalid Theme: '" + $currentTheme + "'";
  }

}

我的主 SASS 文件如下所示:

@import "themer"

div
  @include theme
    background: getValue("ui_main")

但变量 $currentTheme 不会改变。控制台给了我自己的错误:

cmd.exe /D /C call C:\Ruby24-x64\bin\sass.bat --no-cache --update theme.sass:theme.css
      error themer.scss (Line 38: Invalid Theme: '')

【问题讨论】:

    标签: html css sass scss-mixins


    【解决方案1】:

    从局部作用域修改全局变量时,应使用!global 修饰符:

    @mixin theme() {
      $currentTheme: "light" !global;
      .light & {
        @content
      }
    
      $currentTheme: "dark" !global;
      .dark & {
        @content
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-06-23
      • 1970-01-01
      • 2021-10-01
      • 2013-04-15
      • 2013-02-28
      • 2013-12-12
      • 2013-10-28
      • 1970-01-01
      相关资源
      最近更新 更多