简体   繁体   English

如何消除 Angular 14 中 SCSS/SASS 导入的相对路径?

[英]How to eliminate relative paths for SCSS/SASS imports in Angular 14?

I've seen similar questions asked but none have resolved my issue...我看到过类似的问题,但没有人解决我的问题......

I'm trying to eliminate relative paths in my SCSS imports.我正在尝试消除我的 SCSS 导入中的相对路径。 So instead of:所以而不是:

@use "../../../../styles/abstracts/mixins" as queries;

I'd like it to be:我希望它是:

@use "mixins" as queries;

I found this article but their solution does not work.我找到了这篇文章,但他们的解决方案不起作用。

My angular.json file:我的 angular.json 文件:

"root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/email",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "assets": [
              "src/assets"
            ],
            "styles": [
              "src/styles/main.css"
            ],
            "stylePreprocessorOptions": {
              "includePaths": [
                "src/styles/abstracts",
                "src/styles/base",
                "src/styles/layout",
                "src/styles/themes"
              ]
            },
            "scripts": []
          },

I still receive the error我仍然收到错误

Error: Can't find stylesheet to import.
1 │ @import "src/styles/abstracts/mixins";
  │         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  ╵
  page-not-found.component.scss 1:9  root stylesheet

Is it even possible to eliminate relative paths in Angular, and if so how do I accomplish this?是否甚至可以消除 Angular 中的相对路径,如果可以,我该如何实现? Like the question states, I'm using Angular v14.就像问题状态一样,我使用的是 Angular v14。

Your includePaths already contains src/styles/abstracts so you don't need to repeat that in your import.您的includePaths已经包含src/styles/abstracts所以您不需要在导入时重复。 You can just do @import "mixins";你可以做@import "mixins"; and it will look for a mixins file in any of your includePath directories.它会在你的任何includePath目录中寻找一个 mixins 文件。

The way you have it setup right now, Angular is basically trying to look for a file located at: src/styles/abstracts/src/styles/abstracts/mixins您现在设置它的方式,Angular 基本上是在尝试查找位于以下位置的文件: src/styles/abstracts/src/styles/abstracts/mixins

If you want to be a bit clearer in your imports you can change angular.json's config to look like:如果您想在导入时更清楚一点,可以将 angular.json 的配置更改为如下所示:

"includePaths": [
  "src/styles"
]

and use this import:并使用此导入:

@import "abstracts/mixins";

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

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