使用 SASS 时如何从不同目录导入文件? [英] When using SASS how can I import a file from a different directory?

查看:28
本文介绍了使用 SASS 时如何从不同目录导入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 SASS 中,是否可以从另一个目录导入文件?例如,如果我有这样的结构:

In SASS, is it possible to import a file from another directory? For example, if I had a structure like this:

- root_directory
    - sub_directory_a
        - _common.scss
        - template.scss
    - sub_directory_b
        - more_styles.scss

template.scss 可以使用 @import "common" 导入 _common.scss 但是 more_styles.scss 是否可以导入 _common.scss?我尝试了一些不同的东西,包括 @import "../sub_directory_a/common"@import "../sub_directory_a/_common.scss" 但似乎没有任何效果.

template.scss could import _common.scss using @import "common" but is it possible for more_styles.scss to import _common.scss? I tried a few different things including @import "../sub_directory_a/common" and @import "../sub_directory_a/_common.scss" but nothing seems to work.

推荐答案

UPDATE:请考虑 Mikka 的回答 首先 - 如果您只对包含子目录感兴趣,它应该可以正常工作.我的答案特定于包含目录其他而不是子目录,但也适用于这些目录.但是:这不是惯用的方法.

UPDATE: Please consider Mikka's answer first - it should work without flaw if you're only interested in including subdirectories. My answer is specific to including directories other than subdirectories, but works for those, too. But: It's not the idiomatic way to do it.

看起来对 SASS 的一些更改使您最初尝试做的事情成为可能:

Looks like some changes to SASS have made possible what you've initially tried doing:

@import "../subdir/common";

我们甚至让它在位于 c:projectssass 的一些完全不相关的文件夹中工作:

We even got this to work for some totally unrelated folder located in c:projectssass:

@import "../../../../../../../../../../projects/sass/common";

只需添加足够的 ../ 以确保您最终会到达驱动器根目录,并且一切顺利.

Just add enough ../ to be sure you'll end up at the drive root and you're good to go.

当然,这个解决方案远非漂亮,但我无法从完全不同的文件夹导入工作,既不使用 I c:projectssass 也不设置环境变量SASS_PATH(来自::load_paths 参考) 到相同的值.

Of course, this solution is far from pretty, but I couldn't get an import from a totally different folder to work, neither using I c:projectssass nor setting the environment variable SASS_PATH (from: :load_paths reference) to that same value.

这篇关于使用 SASS 时如何从不同目录导入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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