angular.json include Paths 不适用于 node_modules

标签 angular sass

我有一个 Angular 库,其中包含我想使用的样式,就好像它们是应用程序的一部分一样。 我尝试使用 includePaths 来通过在 angular.json 中使用以下配置来做到这一点:

{
    projects: {
        "my-project": {
                "root": "",
                "sourceRoot": "src",
                "prefix": "mp",
                "architect": {
                    "build": {
                        "builder": "@angular-devkit/build-angular:browser",
                        "options": {
                            "outputPath": "dist/selenium-agent-ui",
                            "index": "src/index.html",
                            "main": "src/main.ts",
                            "polyfills": "src/polyfills.ts",
                            "tsConfig": "tsconfig.app.json",
                            "stylePreprocessorOptions": {
                                "includePaths": [
                                    "src/assets",
                                    "node_modules/ui-library/assets"
                                ]
                            },
...

在我的 scss 样式文件中,我想编写以下内容:

@import "assets/ui-library-folder/ui-library-scss-file"

但是,我收到以下错误:

Error: Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
SassError: Can't find stylesheet to import.
  ╷
1 │ @import "assets/ui-library-folder/ui-library-scss-file";
  │         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  ╵
  src\app\app.component.scss 1:9  root stylesheet

文件夹结构:

my-app
|--node_modules
|  |--ui-library
|  |  |--assets
|  |  |  |--ui-library-folder
|  |  |  |  |--ui-library-scss-file.scss
|--src
|  |--app
|  |  |--app.component.scss
|  |--assets
|  |  |--...
|--angular.json
|--package.json
|--tsconfig.json
|--tsconfig.app.json

我已尝试根据这些链接中提供的解决方案进行工作,但它们不起作用:

https://www.digitalocean.com/community/tutorials/angular-shortcut-to-importing-styles-files-in-components

Angular 8 ignores includePaths

Shorten SCSS import path in Angular 7

https://github.com/angular/angular-cli/issues/5798

How to short path to file in Angular?

最佳答案

问题是包含 Assets 的路径

替换:

"src/assets",
"node_modules/ui-library/assets"

"src",
"node_modules/ui-library"

更改导入路径。即:

@import "ui-library-folder/ui-library-scss-file";

而不是:

@import "assets/ui-library-folder/ui-library-scss-file"

关于angular.json include Paths 不适用于 node_modules,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69218193/

相关文章:

angular - 将数据传递到 ionic2 菜单

css - 总是在最后一个位置订购 flex 元素

Sass 将变量名插入字符串

sass - 是否可以将 .css 文件中定义的类 @extend 到 .scss 文件?

Angular FormArray插入

angular - Sass Angular + 主题

node.js - 如何解决构建 Angular 应用程序时发生的此错误?

css - 遍历目录中的文件

angular - 如何动态更改 Angular/RxJS 间隔的周期/延迟

json - 使用 json 模式的 Angular 4 动态组件加载