Angular 4 Material 2 SASS编译错误“导入未找到或不可读的文件”

前端之家收集整理的这篇文章主要介绍了Angular 4 Material 2 SASS编译错误“导入未找到或不可读的文件”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用自定义主题,但我一直收到错误
{
  "status": 1,"file": "/home/adam/Projects/test-material/src/unicorn.scss","line": 1,"column": 1,"message": "File to import not found or unreadable: ~@angular/material/theming.\nParent style sheet: /home/adam/Projects/test-material/src/unicorn.scss","formatted": "Error: File to import not found or unreadable: ~@angular/material/theming.\n       Parent style sheet: /home/adam/Projects/test-material/src/unicorn.scss\n        on line 1 of src/unicorn.scss\n>> @import '~@angular/material/theming';\n   ^\n"
}@H_301_2@ 
 

我已经安装了一个白兰地新的Angular来测试它,我一直得到同样的错误.
我试过了:

$ng new test-material
$cd test-material/
$npm install --save @angular/material@H_301_2@ 
 

然后我创建了一个名为src / unicorn.scss的文件并粘贴了https://material.angular.io/guide/theming中的示例自定义主题并尝试使用以下命令编译它:

$node-sass src / unicorn.scss dist / unicorn.css

我得到了前面提到的错误.
unicorn.scss的内容是:

@import '~@angular/material/theming';
@include mat-core();
$candy-app-primary: mat-palette($mat-indigo);
$candy-app-accent:  mat-palette($mat-pink,A200,A100,A400);
$candy-app-warn:    mat-palette($mat-red);
$candy-app-theme: mat-light-theme($candy-app-primary,$candy-app-accent,$candy-app-warn);
@include angular-material-theme($candy-app-theme);@H_301_2@ 
 

我正在使用Angular 4和Material 2.0.0-beta.3
这是我的package.json:

{
  "name": "test-material","version": "0.0.0","license": "MIT","scripts": {
    "ng": "ng","start": "ng serve","build": "ng build","test": "ng test","lint": "ng lint","e2e": "ng e2e"
  },"private": true,"dependencies": {
    "@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.3","@angular/platform-browser": "^4.0.0","@angular/platform-browser-dynamic": "^4.0.0","@angular/router": "^4.0.0","core-js": "^2.4.1","rxjs": "^5.1.0","zone.js": "^0.8.4"
  },"devDependencies": {
    "@angular/cli": "1.0.0","@angular/compiler-cli": "^4.0.0","@types/jasmine": "2.5.38","@types/node": "~6.0.60","codelyzer": "~2.0.0","jasmine-core": "~2.5.2","jasmine-spec-reporter": "~3.2.0","karma": "~1.4.1","karma-chrome-launcher": "~2.0.0","karma-cli": "~1.0.1","karma-jasmine": "~1.1.0","karma-jasmine-html-reporter": "^0.2.2","karma-coverage-istanbul-reporter": "^0.2.0","protractor": "~5.1.0","ts-node": "~2.0.0","tslint": "~4.5.0","typescript": "~2.2.0"
  }
}@H_301_2@
解决了前面的问题如下.
第一步:
在src目录中创建customTheme.scss.
@import '../node_modules/@angular/material/_theming';

@include mat-core();

$candy-app-primary: mat-palette($mat-cyan);
$candy-app-accent:  mat-palette($mat-green,$candy-app-warn);
@include angular-material-theme($candy-app-theme);@H_301_2@ 
 

重要提示:import语句取决于angular材质版本,import语句位置也取决于customTheme.scss位置.

第二步:
将customTheme.scss添加到.angular-cli.json中的样式数组

styles": [
        "styles.css","customTheme.scss"
      ],@H_301_2@
原文链接:https://www.f2er.com/angularjs/140287.html

猜你在找的Angularjs相关文章