更改Angular 2的材质设计主题

que*_*404 6 material-design angular

我正在尝试将Angular Material 2与我的Angular 2应用程序一起使用.

我没有找到如何全局更改材料主题(primaryColor等).我知道Angular Material 2仍处于alpha状态,但目前有办法吗?

K. *_*ite 5

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

(编辑)-目前已测试并且可以在Angle 7+中运行。

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

在theme.scss文件中,包括一个默认主题(注意,它没有保留在类名下-这是Angular将其用作默认名称),然后是一个明暗主题。

theme.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)
}
Run Code Online (Sandbox Code Playgroud)

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

}
Run Code Online (Sandbox Code Playgroud)

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 {}
Run Code Online (Sandbox Code Playgroud)

最后,从您的视图中调用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>
Run Code Online (Sandbox Code Playgroud)

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


Gün*_*uer 4

https://github.com/angular/material2/issues/287

@samio80 目前,这些样式是在考虑主题的情况下编写的,但我们还没有准备好主题的部署策略。同时,作为一种解决方法,您可以直接拉取源代码并通过修改 _default-theme.scss 并创建 npm 包(通过脚本 stage-release.sh)来自定义主题。

请记住,我们仍处于 alpha 流程的早期阶段,因此 API 或行为可能会在不同版本之间发生变化。