如何在Angular 6或7中从样式.less文件内的组件LESS文件中访问LESS变量?

4

Angular版本7.0.0 文件夹结构是在运行“ng new”时生成的,它是:

这是我的styles.less文件

@personal-black: #0000;

这是我的 app.component.less 文件

#personal-style-scheme {
    color: @personal-black;
}

<h1 id="personal-style-scheme">
    Welcome to {{ title }}!
</h1>

我收到的错误信息是:
``` 编译失败。 ./src/app/app.component.less 模块构建失败(来自./node_modules/less-loader/dist/cjs.js):#personal-style-scheme { color: @personal-black; ^ 变量@personal-black在C:\Users\Sameer\Documents\my-app\src\app\app.component.less(第2行,第11列)中未定义 ```
我不明白为什么组件的less文件无法访问变量@personal-black。
Angular.json文件:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "my-app": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {
        "@schematics/angular:component": {
          "styleext": "less"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/my-app",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.less"
            ],
            "scripts": []
          },
          "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": "my-app:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "my-app:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "my-app: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.less"
            ],
            "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/**"
            ]
          }
        }
      }
    },
    "my-app-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "prefix": "",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "my-app:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "my-app:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "my-app"
}


你好,能否分享一下你的 angular.json 文件? - t3__rry
编辑了答案以包含它。 - Sameer Basil
你需要将这个变量导入到你的 app.component.less 文件中。 @import "styles.less"; ## <- 放在文件顶部。 - jriver27
1个回答

3
我尝试了由@jriver27评论的解决方案,但是当我使用相对路径引用材料主题css时出现以下错误:

ERROR in Module build failed (from ./node_modules/postcss-loader/src/index.js): Error: Failed to find '../../~@angular/material/prebuilt-themes/indigo-pink.css'

我使用import语句的“reference”选项解决了这个问题。你可以在这里找到更多详细信息:这里
@import (reference) "../../styles.less";

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接