2016-04-29 19 views
5

角度2のアプリケーションでAngular Material 2を使用しようとしています。角度2の素材デザインのテーマを変更

グローバルなテーマテーマの変更方法(primaryColorなど)が見つかりませんでした。 角度材2はまだアルファになっていますが、現在はその方法がありますか? samio80スタイル@

+0

角材1の文書を読んだことがありますか?多分それは似ています。まだ2のドキュメントがない場合は、ソースコードを見て調べてみてください。または、githubプロジェクトで問題を提起してください。 –

答えて

3

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

現在、心の中でテーマに書かれているが、我々はまだ準備ができてテーマ設定のための展開戦略を持っていません。回避策として、_default-theme.scssを修正し、npmパッケージを作成する(stage-release.shスクリプトを使用して)ソースを直接プルしてテーマをカスタマイズすることができます。

私たちはまだアルファプロセスの初期段階であり、APIや動作はリリース間で変更される可能性があることに注意してください。 https://material.angular.io/guide/theming

そして、ここではガイドの説明テーマのアプローチを実装するサンプルアプリです - - ここで

+0

ありがとう、ありがとう:)ベータ版のリリース日について考えていますか? – quen2404

+0

いいえ、Googleはタイムラインを提供していません=>「準備ができたら」 –

+0

@GünterZöchbauerテーマからスタイルをコピーしてコンポーネントのスタイルシートに追加するのは、テーマを使用する正しい方法ですか? – blackHawk

1

ここで動的な角度素材テーマの実装の一例ですアンギュラ5.1およびアンギュラマテリアル5.0の場合そして、そして - (これがデフォルトとして使用しますので、角張っていること、クラス名の下に保管されていないに注意してください)デフォルトのテーマが含まれ、theme.scssファイルでhttps://stackblitz.com/edit/dynamic-material-theming

-

編集可能な例を作業します明暗のテーマ。 app.componentファイルで

theme.scss

@import '[email protected]/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) 
} 

、角度/ CDK /オーバーレイ@からOverlayContainerが含まれます。このためのAngularのドキュメントはここにありますhttps://material.angular.io/guide/theming;それらの実装は少し異なります。また、app.moduleにもインポートとしてOverlayModuleを含める必要がありました。

私のapp.componentファイルでは、@HostBinding('class') componentCssClass;も変数として宣言しました。この変数はテーマをクラスとして設定するために使用されます。 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 {} 

最後に、あなたのビューからonSetTheme関数を呼び出す

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.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> 

あなたは機能がよりダイナミックになるように観察を使用して検討するかもしれません。

+0

これは答えの 質問に答えるかもしれませんが、答えの の本質的な部分を含めることが望ましいです(http://meta.stackoverflow.com/q/8259)。 –

+0

申し訳ありませんが、私は記事の詳細を提供しました。 –

+0

複数の質問に同じ回答を投稿しないでください。 1つの良い答えを投稿し、投票/フラグを立てて他の質問を重複として閉じます。質問が重複でない場合は、*あなたの質問に対する回答を調整してください。* –

関連する問題