【发布时间】:2017-09-30 05:09:23
【问题描述】:
我有一个使用 Material 2(当前版本,2.0.0-beta.11)的 Angular 4 应用程序。
我按照documentation进行主题化,创建了两个主题,一个浅色主题和一个深色主题。因为我不使用 md-sidenav 组件,所以我将 mat-app-background 类放在 body 元素上,它应用当前主题的背景颜色(浅灰色用于浅色主题,深灰色用于深色主题。)
问题是当我的主题切换器被切换时(一个添加或删除类的简单按钮,没什么特别的,like this answer),mat-app-background 的背景颜色没有更新。
我无法创建成功使用自定义主题的 plunker,但基本要点如下:
index.html
<body class="mat-app-background">
<app-root></app-root>
</body>
app.component.html
<main [class.dark-theme]="isDarkTheme">
<button md-raised-button color="primary" (click)="toggleTheme()">Toggle</button>
<!-- other content -->
</main>
app.component.ts
isDarkTheme: boolean = false;
toggleTheme() { this.isDarkTheme = !this.isDarkTheme; }
主题.scss
@import '~@angular/material/theming';
@include mat-core();
// light (default) theme
$lt-primary: mat-palette($mat-indigo);
$lt-accent: mat-palette($mat-pink, A200, A100, A400);
$lt-theme: mat-light-theme($lt-primary, $lt-accent);
@include angular-material-theme($lt-theme);
// alternate dark theme
$dark-primary: mat-palette($mat-blue-grey);
$dark-accent: mat-palette($mat-amber, A200, A100, A400);
$dark-warn: mat-palette($mat-deep-orange);
$dark-theme: mat-dark-theme($dark-primary, $dark-accent, $dark-warn);
.dark-theme {
@include angular-material-theme($dark-theme);
}
当我单击按钮时,它成功切换了主题:应用了类,按钮将颜色从浅色主题原色更改为深色主题原色。但背景颜色仍然是浅色主题的背景颜色。
默认背景颜色“固定”并且不受主题切换的影响。我缺少什么?有没有办法让背景颜色自动在没有混合/手动重新定义的情况下更新?
(注意:干净的 Angular cli 项目,使用直接从 Angular 材质文档粘贴的主题副本。Angular 4.4.4,材质 2.0.0-beta.11。)
【问题讨论】: