简体   繁体   English

如何在Angular应用程序中导入带有资源的SCSS库?

[英]How to import SCSS library with assets in Angular application?

I have a SCSS library packaged as npm module, it has the following structure: 我有一个打包为npm模块的SCSS库,它具有以下结构:

./index.scss
./partials/_foo.scss
./assets/foo.svg

And the _foo.scss contains a relative path to the image: _foo.scss包含图像的相对路径:

.foo {
  background-image: url('../assets/foo.svg');
}

When I import the index.scss file in my Angular application's style.scss , I'm getting a compilation error, because the compiler can't resolve the path to the image for some reason, however, the path is correct (relative to the original SCSS-file). 当我在Angular应用程序的style.scss导入index.scss文件时,出现编译错误,因为编译器由于某种原因无法解析图像的路径,但是,该路径是正确的(相对于原始SCSS文件)。

  1. Is there a way to help Angular to resolve such images correctly? 有没有办法帮助Angular正确解析此类图像?
  2. Is there a way to improve the library somehow to make it easier to use in Angular? 有没有办法以某种方式改进库,使其更易于在Angular中使用?

I know I can use a variable to specify the base path in the library and then override it in the project where I import it, but I rather won't do it this way, because it adds more clutter to the library itself and requires the end-developer to override the variable in order to use the library. 我知道我可以使用变量在库中指定基本路径,然后在导入它的项目中覆盖它,但是我宁愿不这样做,因为它会给库本身增加更多混乱,并且需要最终开发人员将覆盖该变量以使用该库。

如果您使用url('~nameofnodemodule/assets/foo.svg') webpack应该能够找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM