我使用CLI创建了一个Angular项目。我使用的是SCSS,并且我包含了带有自定义主题iirc的Angular材质。我添加了几个虚拟组件,应用程序仍然构建得很好。然后我需要使用Angular材质设置我的组件的样式。为此,我在style.scss文件的第一行添加了@use '~@angular/material' as mat;。一旦我这样做了,应用程序将不再构建。它总是抛出以下错误:
ERROR in ./src/styles.scss (./node_modules/css-loader/dist/cjs.js??ref--13-1!./node_modules/postcss-loader/src??embedded!./node_modules/resolve-url-loader??ref--13-3!./node_modules/sass-loader/dist/cjs.js??ref--13-4!./src/styles.scss)
Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
SassError: Can't find stylesheet to import.
╷
1 │ @use '~@angular/material' as mat;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
╵
src/styles.scss 1:1 root stylesheet我不知道我做错了什么;我的印象是,以这种方式导入角度材质就可以了。我做错了什么吗?
下面是我的整个style.scss文件,如果有用的话:
@use '~@angular/material' as mat;
// Custom Theming for Angular Material
// For more information: https://material.angular.io/guide/theming
@import '~@angular/material/theming';
// Plus imports for other components in your app.
// Include the common styles for Angular Material. We include this here so that you only
// have to load a single css file for Angular Material in your app.
// Be sure that you only ever include this mixin once!
@include mat-core();
// For each palette, you can optionally specify a default, lighter, and darker hue.
$aphrodite-primary: mat-palette($mat-indigo);
$aphrodite-accent: mat-palette($mat-pink, A200, A100, A400);
// The warn palette is optional (defaults to red).
$aphrodite-warn: mat-palette($mat-red);
// Create the theme object. A theme consists of configurations for individual
// theming systems such as "color" or "typography".
$aphrodite-theme: mat-light-theme((
color: (
primary: $aphrodite-primary,
accent: $aphrodite-accent,
warn: $aphrodite-warn,
)
));
// Include theme styles for core and each component used in your app.
// Alternatively, you can import and @include the theme mixins for each component
// that you are using.
@include angular-material-theme($aphrodite-theme);
/* You can add global styles to this file, and also import other style files */
@import '~normalize.css';
@import 'sassVars.scss';
html, body { height: 100%; }
html{
background: $nullGray;
min-width: 400px;
}
body {
font-family: Roboto, "Helvetica Neue", sans-serif;
background: $canvas;
max-width: $bodyWidth;
margin: auto;
height: 100%;
@include elevation(16);
}这是我的angular.json文件:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"myapp": {
"projectType": "application",
"schematics": {
"@schematics/angular:component": {
"style": "scss"
}
},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"baseHref" : "/app/",
"deployUrl": "/app/",
"outputPath": "dist/myapp",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"aot": true,
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.scss"
],
"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,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "6kb",
"maximumError": "10kb"
}
]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "myapp:build"
},
"configurations": {
"production": {
"browserTarget": "myapp:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "myapp:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.scss"
],
"scripts": []
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"tsconfig.app.json",
"tsconfig.spec.json",
"e2e/tsconfig.json"
],
"exclude": [
"**/node_modules/**"
]
}
},
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "myapp:serve"
},
"configurations": {
"production": {
"devServerTarget": "myapp:serve:production"
}
}
}
}
}
},
"defaultProject": "myapp",
"cli": {
"analytics": "0ded4b78-d900-44ea-8ad2-b5cbba677e06"
}
}发布于 2021-06-05 23:08:07
与Google API一样,Angular Material版本11和版本12之间存在混淆。
简短的回答
在SCSS中,他们似乎反对使用支持@use语法的@import。看起来Angular Material在v11->12的某个地方实现了这个改变。
(我也是个新手。这是我最好的猜测)。
Watch this YouTube video获取不同之处的大致概念。
There is a reason why Google Angular is the most dreaded framework of 2020 on Stack Overflow :),因此它取决于您正在使用的角度材质版本
长长的答案
在Angular Material v11中,他们似乎使用@import语法,而在v12中,他们似乎更喜欢@use语法。
因此,您似乎正在尝试使用使用@import语法的Angular Material v11设计的教程或主题,并将其与Angular Material v12的@use语法混合在一起。两者之间最大的区别在于,@use语法会导致在SCSS前面加上前缀,因此函数名称会稍有不同,例如
//angular material v11 which uses @import syntax
@import '~@angular/material/theming';//notice that the file imported is also different
@include mat-core();
$candy-app-primary: mat-palette($mat-indigo); //notice that functions not Namespaced
$candy-app-accent: mat-palette($mat-pink, A200, A100, A400);而使用使用@use语法的angular材质v12的相同示例
@use '~@angular/material' as mat;
$my-primary: mat.define-palette(mat.$indigo-palette, 500); //notice that functions are prefixed with Namespace mat. This is a feature of the scss @use directive
$my-accent: mat.define-palette(mat.$pink-palette, A200, A100, A400);我还没有找到在两个版本中找到正确的函数名的方法。手动迁移主题不是一件容易的事,特别是当你是一个新手的时候
因此,您可以看到,根据您使用的是Material v11还是12,很容易丢失样式表。

发布于 2021-11-08 20:41:40
对于Angular 13,尝试从路径中删除代字号(~),这样导入:
@use '@angular/material' as mat;它就像一种护身符。
发布于 2021-05-23 01:06:34
显然,我读错了我的版本的文档。上面的代码有两点需要更改,才能让它为我工作。
你不需要做@use '~@angular/material' as mat;。重要的一行是@import '~@angular/material/theming';,它已经由CLI放入文件中。
这不是@include mat-elevation(16);.,是
@include elevation(16);https://stackoverflow.com/questions/67652012
复制相似问题