使用变量 SASS 导入路径 [英] Import path using variable SASS

查看:35
本文介绍了使用变量 SASS 导入路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用变量导入文件.

I'd like to import a file using variables.

我的代码(不起作用):

My code (that doesn't work) :

$input-field-theme: default;
@import '#{$input-field-theme}';

错误消息:错误:未找到要导入的文件或无法读取:#{$input-field-theme}

这有效:@import 'default';

推荐答案

目前无法解决,请查看 2012 年针对 Sass 记录的 GitHub 问题以获取更多信息.

At this moment it is not possible, check this GitHub issue logged against Sass in 2012 for more information.

推理给出如下:

允许动态导入将完全破坏 Sass 快速确定哪些文件导入哪些其他文件,以及什么时候需要重新编译的能力.

Allowing dynamic imports would completely break Sass's ability to quickly determine which files import which other files, and thus what needs re-compiling when.

该线程还包含指向另一个问题的链接,其中包含此 2018 年评论 讨论动态依赖项的未来计划:

That thread also includes a link to another issue which includes this 2018 comment that discusses future plans for dynamic dependencies:

我暂时锁定这个问题,因为有很多噪音没有增加了很多价值.总而言之,这是计划:

I'm locking this issue for now because there's a lot of noise without a lot of value being added. To summarize, this is the plan:

新的 @use 指令将提供将文件作为mixin,因此您可以动态决定是否包含它以及在何处包含它.这将使 Sass 与其他运行良好的语言更加一致没有动态导入,因为这意味着导入不再具有不可避免的副作用.

The new @use directive will provide the ability to import a file as a mixin, so you can dynamically decide whether and where to include it. This will bring Sass more in line with other languages that work well without dynamic imports, since it means importing no longer has unavoidable side-effects.

我们将添加一个 load() 函数 如上所述,这将允许样式表在运行时根据变量值加载文件.这将支持需要样式表的更复杂的用例基于用户输入加载,同时保留静态加载的能力跟踪导入图及其定义的 mixin 和函数.

We will add a load() function as described above that will allow stylesheets to load files at runtime based on variable values. This will support the more complex use-cases where stylesheets need to be loaded based on user input, while preserving the ability to statically trace the import graph and the mixins and functions it defines.

这篇关于使用变量 SASS 导入路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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