gpt4 book ai didi

angular - 更改 Angular 2 的 Material 设计主题

转载 作者:太空狗 更新时间:2023-10-29 17:24:27 25 4
gpt4 key购买 nike

我正在尝试使用 Angular Material 2使用我的 Angular 2 应用程序。

我没有找到如何全局更改 Material 主题(primaryColor 等)。我知道 Angular Material 2 仍处于 alpha 阶段,但目前有办法做到这一点吗?

最佳答案

这是 Angular 5.1 和 Angular Material 5.0 中动态 Angular Material 主题实现的示例。

(编辑)- 目前正在测试并在 Angular 7+ 中工作。

工作可编辑示例 - https://stackblitz.com/edit/dynamic-material-theming

在 theme.scss 文件中,包括一个默认主题(注意它没有保存在类名下 - 这是 Angular 将使用它作为默认主题),然后是一个浅色和深色主题。

主题.scss

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

// Typography
$custom-typography: mat-typography-config(
$font-family: Raleway,
$headline: mat-typography-level(24px, 48px, 400),
$body-1: mat-typography-level(16px, 24px, 400)
);
@include angular-material-typography($custom-typography);

// Default colors
$my-app-primary: mat-palette($mat-teal, 700, 100, 800);
$my-app-accent: mat-palette($mat-teal, 700, 100, 800);

$my-app-theme: mat-light-theme($my-app-primary, $my-app-accent);
@include angular-material-theme($my-app-theme);

// 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);
}

// Light theme
$light-primary: mat-palette($mat-grey, 200, 500, 300);
$light-accent: mat-palette($mat-brown, 200);
$light-warn: mat-palette($mat-deep-orange, 200);

$light-theme: mat-light-theme($light-primary, $light-accent, $light-warn);

.light-theme {
@include angular-material-theme($light-theme)
}

在 app.component 文件中,包含来自 @angular/cdk/overlay 的 OverlayContainer。您可以在此处找到 Angular 的相关文档 https://material.angular.io/guide/theming ;尽管它们的实现有些不同。请注意,我还必须将 OverlayModule 作为导入包含在 app.module 中。

在我的 app.component 文件中,我还将 @HostBinding('class') componentCssClass; 声明为一个变量,它将用于将主题设置为一个类。

app.component.ts

import {Component, HostBinding, OnInit} from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Version } from './classes/version';
import { OverlayContainer} from '@angular/cdk/overlay';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {

constructor(private http: HttpClient, public overlayContainer: OverlayContainer) {}

title = 'app';
version: Version;
@HostBinding('class') componentCssClass;

ngOnInit() {
this.getVersion();
}

onSetTheme(theme) {
this.overlayContainer.getContainerElement().classList.add(theme);
this.componentCssClass = theme;
}

getVersion() {
this.http.get<Version>('/api/version')
.subscribe(data => {
this.version = data;
});
}

}

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { HttpClientModule } from '@angular/common/http';

import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatCardModule } from '@angular/material/card';
import { MatButtonModule } from '@angular/material/button';

import { AppComponent } from './app.component';

import { OverlayModule } from '@angular/cdk/overlay';

@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
HttpClientModule,
BrowserAnimationsModule,
MatCardModule,
MatButtonModule,
OverlayModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}

最后,从您的 View 调用 onSetTheme 函数。

app.component.html

<button mat-raised-button color="primary" (click)="onSetTheme('default-theme')">Default</button>
<button mat-raised-button color="primary" (click)="onSetTheme('dark-theme')">Dark</button>
<button mat-raised-button color="primary" (click)="onSetTheme('light-theme')">Light</button>

您可能会考虑使用可观察对象,以便功能更加动态。

关于angular - 更改 Angular 2 的 Material 设计主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36939523/

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