【问题标题】:Sass use variable value in @importSass 在@import 中使用变量值
【发布时间】:2018-06-04 14:44:13
【问题描述】:

我的代码是这样的

      &.village {
        &.village- {
          @for $i from 1 through 12 {
            &#{$i} {
              background-image: url('../../images/maps/#{$i}.png');

              @import "maps/map-#{$i}";
            }
          }
        }
      }

然后我得到这个错误:

模块构建失败:ModuleBuildError:模块构建失败: @import "地图/地图-#{$i}"; ^ 找不到或无法读取要导入的文件:maps/map-#{$i}。

我使用 Laravel 5.6,我已经尝试查找它,但到目前为止我没有找到任何东西。是否可以使用 scss 导入变量文件名?

【问题讨论】:

    标签: sass scss-mixins


    【解决方案1】:

    目前是not possible,但不久的将来可能会向 Sass 添加一些新功能。

    您可以通过使用条件来创建规则以匹配您的导入来实现这一点。您可以为此使用@if(当我们等待@switch 声明为implemented):

    @mixin customImport($file) {
        @if $file == 'file-one' {
            @import 'file-one';
        } @else if $file == 'file-two' {
            @import 'file-two';
        }
    }
    

    然后:

    @include customImport('file-one');
    

    【讨论】:

    • 我尝试了这样的 mixin,但出现以下错误:Import directives may not be used within control directives or mixins。使用 SASS 3.5.6。
    • @JamesJones 我认为你只需要更新你的 sass 编译器版本。
    • 感谢您的回复。 3.5.6 是撰写本文时最新的 Ruby SASS。有什么我想念的吗?
    猜你喜欢
    • 1970-01-01
    • 2019-09-13
    • 2019-10-05
    • 2012-10-29
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 2011-12-29
    • 2012-10-09
    相关资源
    最近更新 更多