到目前为止,我做了以下几件事:
1. 我创建了一个名为my-theme.scss的文件:
@import "../node_module/@angular/material/_theming";
@include mat-core();
$my-theme-primary: mat-palette($mat-amber);
$my-theme-accent: mat-palette($mat-orange, A200, A100, A400);
$my-theme-warn: mat-palette($mat-red);
$my-theme: mat-light-theme($my-theme-primary, $my-theme-accent, $my-theme-warn);
@include angular-material-theme($my-theme);
2. 我将创建的SASS文件导入到styles.css中。
@import "my-theme.scss"
3. 我在 .angular-cli.json 文件的样式部分中引用了它:
"styles": [
"styles.css",
"my-theme.scss"
]
我的本地网站提示我“编译失败”,模块未找到:'../node_modules/@angular/material/_theming'。
在my-theme.scss文件中,我尝试了几种导入路径的变化(“~@angular/material/_theming”,“../node_modules/@angular/material/_theming”,“~@angular/material/theming”,“../node_modules/@angular/material/theming”...)。在Angular文档中,使用“theming”作为位置,但我在某个地方读到,Angular Material将文件移动到了“_theming”,我也在我的文件夹结构中找到了它。
当我使用路径“../node_modules/@angular/material/_theming.scss”时,编译器会找到该文件,但由于另一个错误而导致编译失败:
Module build failed: Missed semicolon (30:1)
28 | // Media queries
29 | // TODO: Find a way to respect media query ranges.
> 30 | // TODO: For example the xs-breakpoint should not interfere with the sm-breakpoint.
| ^
31 | $mat-xsmall: 'max-width: 600px';
32 | $mat-small: 'max-width: 960px';
这对我来说很奇怪,因为_theming.scss文件是一个Angular Material文件,而错误的位置是一个注释。此外,在其他有效的网络实现中,我没有看到任何以.scss结尾的导入语句。
我花了几个小时搜索如何将自定义主题添加到我的网站...我错过了什么吗?我对Angular 2和SASS还很陌生。
PS:我已经有点困惑了,是否应该将my-theme.scss导入styles.css中。 Angular文档没有提及,但我读到一些评论,人们说这解决了他们使用自定义Angular Material主题的问题。
项目结构:
my-project
|--node_modules
| |--@angular
| |--material
| |--prebuild-themes
| |--_theming.scss
|--src
| |--app
| | |--my-component.css | html | ts
| | |--app.module.ts
| |--assets
| |--index.html
| |--main.ts
| |--my-theme.scss
| |--styles.css
|--.angular-cli.json
package.json 文件:
"dependencies": {
"@angular/animations": "^4.3.1",
"@angular/cdk": "^2.0.0-beta.8",
"@angular/common": "^4.0.0",
"@angular/compiler": "^4.0.0",
"@angular/core": "^4.0.0",
"@angular/forms": "^4.0.0",
"@angular/http": "^4.0.0",
"@angular/material": "^2.0.0-beta.8",
"@angular/platform-browser": "^4.0.0",
"@angular/platform-browser-dynamic": "^4.0.0",
"@angular/router": "^4.0.0",
"core-js": "^2.4.1",
"hammerjs": "^2.0.8",
"rxjs": "^5.1.0",
"zone.js": "^0.8.4"
},
"devDependencies": {
"@angular/cli": "1.2.1",
"@angular/compiler-cli": "^4.0.0",
"@angular/language-service": "^4.0.0",
"@types/jasmine": "~2.5.53",
"@types/jasminewd2": "~2.0.2",
"@types/node": "~6.0.60",
"codelyzer": "~3.0.1",
"jasmine-core": "~2.6.2",
"jasmine-spec-reporter": "~4.1.0",
"karma": "~1.7.0",
"karma-chrome-launcher": "~2.1.1",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.2",
"ts-node": "~3.0.4",
"tslint": "~5.3.2",
"typescript": "~2.3.3"
}
Windows 7, 64位 Firefox 52.2.1 Visual Studio Code 1.14.1