无法导入`.scss`文件出错

时间:2019-05-23 14:18:55

标签: angular7

在角度上,我得到以下错误:

ERROR in ./app/shared-components/header/header.component.scss
Module build failed (from ../node_modules/sass-loader/lib/loader.js):

@import '~styles.scss';
^
      File to import not found or unreadable: ~styles.scss.
      in D:\IBO\Project\IBO-UI\NG-IBO\src\app\shared-components\header\header.component.scss (line 4, column 1)
Error:
@import '~styles.scss';

这是我的angular.json文件:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "ibo": {
      "root": "src",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "",
      "schematics": {
        "@schematics/angular:component": {
          "styleext": "scss"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/ibo",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src",
              "src/assets",
              "src/web.config"
            ],
            "stylePreprocessorOptions": {
                "includePaths": [
                  "node_modules/bootstrap",
                  "node_modules/angular-calendar",
                  "./src/styles"
                ]
             },
            "scripts": [
              "node_modules/popper.js/dist/umd/popper.min.js",
              "node_modules/jquery/dist/jquery.min.js",
              "node_modules/bootstrap/dist/js/bootstrap.min.js"
            ],
            "styles": [
              "node_modules/devextreme/dist/css/dx.common.css",
              "node_modules/devextreme/dist/css/dx.light.css",
               "node_modules/font-awesome/css/font-awesome.min.css",
               "src/styles/styles.scss",
            ],
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "ibo:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "ibo:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "ibo:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "./src/styles/styles.scss"
            ],
            "scripts": [],
            "assets": [
              "./src/favicon.ico",
              "./src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "ibo-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "prefix": "app",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "ibo:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "ibo:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "ibo"
}

如何提出这个建议?

谢谢。

1 个答案:

答案 0 :(得分:2)

// Angular CLI: 7.3.9 
// Node: 10.15.0
// OS: win32 x64
// Angular: 7.2.15

嗨,

这是我解决问题的方式。

我创建的文件带有

  

src / theme / _project-variables.scss

然后在我使用的 main.scss component.scss 文件中

@import "~theme/project-variables";

如果您未在文件名中添加“ _”,则它将不会呈现。

此外,您还需要将此文件包含在要使用变量的每个component.scss中。