【发布时间】:2019-04-08 22:17:26
【问题描述】:
我一直在阅读一些关于此的文章,但它们似乎以几种不同的方式相互冲突。我希望为最新版本的角材料 [5.0.0-rc0] 重新创建与 angular material documentation site 相同的主题切换
我有两个自定义主题,一个是 custom-theme.scss,另一个是 light-custom-theme.scss,几乎相同,没有 mat-dark-theme
@import '~@angular/material/theming';
$custom-theme-primary: mat-palette($mat-blue);
$custom-theme-accent: mat-palette($mat-orange, A200, A100, A400);
$custom-theme-warn: mat-palette($mat-red);
$custom-theme: mat-dark-theme($custom-theme-primary, $custom-theme-accent, $custom-theme-warn);
@include angular-material-theme($custom-theme);
我的 styles.scss 看起来像这样
@import '~@angular/material/theming';
@include mat-core();
@import 'custom-theme.scss';
@import 'light-custom-theme.scss';
.custom-theme {
@include angular-material-theme($custom-theme);
}
.light-custom-theme {
@include angular-material-theme($light-custom-theme);
}
然后在index.html中调用<body class="mat-app-background">
当我做一个主题时,一切都很好。但我试图在两者之间切换。将两个主题添加到 angular-cli.json 中,light-custom-theme 接管
"styles": [
"styles.scss",
"custom-theme.scss",
"light-custom-theme.scss"
],
我的一个组件中有以下代码来处理切换主题
toggleTheme(): void {
if (this.overlay.classList.contains("custom-theme")) {
this.overlay.classList.remove("custom-theme");
this.overlay.classList.add("light-custom-theme");
} else if (this.overlay.classList.contains("light-custom-theme")) {
this.overlay.classList.remove("light-custom-theme");
this.overlay.classList.add("custom-theme");
} else {
this.overlay.classList.add("light-custom-theme");
}
}
但无论何时运行,主题都保持不变。值得一提的是,overlay.classList 中的位置 0 处已经有一个“cdk-overlay-container”对象
0:"cdk-overlay-container"
1:"custom-theme"
length:2
value:"cdk-overlay-container custom-theme"
我不确定如何调试它,因为角度材料文档并没有给我太多的工作,任何帮助都将不胜感激!
谢谢!
【问题讨论】:
标签: angular sass angular-material angular5