gpt4 book ai didi

Angular Material 5 深色主题未应用于 body

转载 作者:行者123 更新时间:2023-12-04 22:18:24 25 4
gpt4 key购买 nike

我创建了一个“自定义”主题,(使用 https://material.angular.io/guide/theming 上的主题文档,这很糟糕),如下所示:

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

$ip-primary: mat-palette($mat-indigo);
$ip-accent: mat-palette($mat-pink, A200, A100, A400);
$ip-theme: mat-light-theme($ip-primary, $ip-accent);
$ip-theme-dark: mat-dark-theme($ip-primary, $ip-accent);

.indigo-pink {
@include angular-material-theme($ip-theme);
}

.indigo-pink-dark {
@include angular-material-theme($ip-theme-dark);
}

我的 index.html 包含这个:
<body class="mat-app-background mat-typography">
<app-root></app-root>
</body>

在容器中为 app-root组件,我使用这个在我的自定义类名( indigo-pinkindigo-pink-dark )之间切换:
<div [ngClass]="appTheme">

我还使用以下命令将 CDK 覆盖容器类设置为我的自定义类名:
setContainerClass(className: string): void {
const containerClassToKeep = 'cdk-overlay-container';
const overlayClassList = this.overlayContainer.getContainerElement().classList;
const existingClasses = Array.from(overlayClassList);

existingClasses.forEach(classToken => {
if (classToken !== containerClassToKeep) {
overlayClassList.remove(classToken);
}
});

overlayClassList.add(className);
}

主要问题是当我切换到深色主题时,我确实看到了 indigo-pink-dark类名被正确添加到我的应用程序组件容器中,但只有一些样式发生了变化(例如顶部的工具栏变暗, Material 组件) - 页面的主体保持白色。主题指南(和其他博客文章)说使用 mat-app-background应该解决这个确切的问题。

此外,如果我尝试手动更新主体的背景颜色,例如使用
.indigo-pink-dark {
@include angular-material-theme($ip-theme-dark);

body & {
background-color: #000;
}
}

然后这个“背景颜色”只是覆盖整个页面,你看不到页面上的任何元素。这与 CDK 覆盖层有关,因为如果我删除将类名添加到覆盖层的部分,它会起作用 - 页面的背景确实设置正确,但是覆盖层没有获得正确的样式。

最佳答案

我遇到了同样的问题,发现 this solution on the Angular Material GitHub issue#3298 (post by JamieStill) .

我将所有 app.component.html 内容都包裹在一个 div 中

<div class="mat-typography app-frame mat-app-background">
<app-header></app-header>
<main>
<app-post-create></app-post-create>
<app-post-list></app-post-list>
</main>
</div>

在 app.component.css 中
html, body, app-root, .app-frame {
overflow: hidden;
margin: 0;
height: 100%;
box-sizing: border-box;
color: #e0e0e0;
}

我只在 Angular 6 的练习应用程序中测试过这个,但我怀疑它也适用于 Angular 5。

之前:

Before

之后:

After

关于 Angular Material 5 深色主题未应用于 body ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49734551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com