【问题标题】:Not able to use a custom theme in Angular 4无法在 Angular 4 中使用自定义主题
【发布时间】:2018-05-27 03:40:24
【问题描述】:

我正在尝试在我的 Angular 项目中使用我的自定义主题。它没有按预期工作,我收到以下错误:

./node_modules/css-loader?{"sourceMap":false,"importLoaders":1}!./node_modules/postcss-loader?{"ident":"postcss"}!./src/custom_theme.scss
Module not found: Error: Can't resolve '@angular/material/theming' in '/Users/mathiasschrooten/Downloads/skylux-admin/src'

我的 style.css 文件:

@import "custom_theme.scss"

.mat-tab-label {
  flex: 1 1 auto;
}

我的 custom_theme.scss 文件:

@import '~@angular/material/theming';
@include mat-core();

$candy-app-primary: mat-palette($mat-orange);
$candy-app-accent:  mat-palette($mat-orange, 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);

.angular-cli.json

      "styles": [
    "styles.css",
    "custom_theme.scss"
  ],

提前致谢!

【问题讨论】:

  • @MathijsSegers 我执行了 'npm install --save @angular/material @angular/cdk' 和 'npm install --save @angular/animations'。还有什么我应该导入的吗?根据应该全部的文档,对吗?

标签: angular sass angular-material2


【解决方案1】:

您可以从styles.css 中删除@import "custom_theme.scss"。由于您已将 custom_theme.scss 添加到您的 angular-cli.json(或 v6+ 的 angular.json),因此不需要导入。

编辑angular.json 文件后,您需要再次运行ng-serve

根据official docs:

如果您使用的是 Angular CLI,则支持将 Sass 编译为 css 内置;您只需在“样式”列表中添加一个新条目 angular-cli.json 指向主题文件(例如, unicorn-app-theme.scss)。

【讨论】:

  • 没有更多错误,但 Angular 似乎无法识别任何主题(没有任何颜色可见,组件无法按预期工作)
  • 尝试关闭服务器并使用ng-serve重建
  • 谢谢兄弟,不敢相信重启服务器就成功了
  • 请添加重启服务器作为正确答案! :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-18
  • 1970-01-01
  • 1970-01-01
  • 2017-10-21
  • 2012-07-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多