gpt4 book ai didi

javascript - 如何从 Angular 6 或 7 组件内的 LESS 文件访问 style.less 中的 LESS 变量?

转载 作者:行者123 更新时间:2023-12-01 16:29:38 25 4
gpt4 key购买 nike

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>


我得到的错误是:

Failed to compile.

./src/app/app.component.less Module build failed (from ./node_modules/less-loader/dist/cjs.js): #personal-style-scheme { color: @personal-black; ^ Variable @personal-black is undefined in C:\Users\Sameer\Documents\my-app\src\app\app.component.less (line 2, column 11)



我不明白为什么组件的 less 文件无法访问 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"
}

最佳答案

我尝试了 @jriver27 评论的解决方案,但是当我使用相对路径引用 Material 主题 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'



我使用导入语句的“引用”选项修复了该问题。您可以在 here 中找到更多详细信息。
@import (reference) "../../styles.less";

关于javascript - 如何从 Angular 6 或 7 组件内的 LESS 文件访问 style.less 中的 LESS 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53263619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com