css - Sass 无法使用 "~"从 node_modules 文件夹导入

标签 css angular sass

我正在使用 Angular 6 开发单页 Web 应用程序, 我添加了以下 ngx-toast library到我的元素。

当我将以下 Sass 添加到我的元素中并且当我使用“~”而不是相对路径时,它无法加载库:

   // regular style toast 
@import '~ngx-toastr/toastr.css';

// bootstrap style toast 
// or import a bootstrap 4 alert styled design (SASS ONLY) 
// should be after your bootstrap imports, it uses bs4 variables, mixins, functions 
@import '~ngx-toastr/toastr-bs4-alert';

// if you'd like to use it without importing all of bootstrap it requires 
@import '~bootstrap/scss/functions';
@import '~bootstrap/scss/variables';
@import '~bootstrap/scss/mixins';
@import '~ngx-toastr/toastr-bs4-alert';

但是当我使用相对路径时它是有效的:

// regular style toast 
@import '../../node_modules/ngx-toastr/toastr.css';

// bootstrap style toast 
// or import a bootstrap 4 alert styled design (SASS ONLY) 
// should be after your bootstrap imports, it uses bs4 variables, mixins, functions 
@import '../../node_modules/ngx-toastr/toastr-bs4-alert';

// if you'd like to use it without importing all of bootstrap it requires 
@import '../../node_modules/bootstrap/scss/functions';
@import'../../node_modules/bootstrap/scss/variables';
@import '../../node_modules/bootstrap/scss/mixins';
@import '../../node_modules/ngx-toastr/toastr-bs4-alert';

我的组件模块

   @Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})

当然,我可以保留它,或者只是从手册中下载实际的 css,但令我困扰的是它无法导入,因为它应该可以工作。

有什么解决办法吗?

最佳答案

根据 SASS 文档,当我们导入任何 SCSS 文件时,~ 将指向 src/ 文件夹。当我们导入任何 sass 文件时,我们需要告诉 Angular 包含 node_modules/ 路径。这可以使用 angular.json 中的 stylePreprocessorOptions 属性来实现。

"styles": [
  ...
],
"stylePreprocessorOptions": {
  "includePaths": [
    "../node_modules/ngx-toastr"
  ]
} 

关于css - Sass 无法使用 "~"从 node_modules 文件夹导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53019465/

相关文章:

angular - 错误 : Cannot match any routes. URL 段:- Angular 2

google-maps - ionic 3 : google map hides if background overwritten

sass - 在 VS Code 的 .vue 文件中为 Sass 启用 IntelliSense

css - 为文本制作带有渐变的底部边框

css - 动画边框效果不适用于 IE

angular - APP_INITIALIZER 不阻止导入的模块

angular - 如何在垫子工具提示 div 下放置一个三 Angular 形?

html - 使用 rem 时,Bootstrap 标题字体在移动设备中较小

jquery - 如何更改 Magnific Popup 中的 CSS 属性

css - 向 CSS 颜色变量添加不透明度