2017-03-14 23 views
5

私自身のscssテーマを作成し、angular-cli.jsonで宣言しました。すべてうまくいきます。角2の素材動的テーマ

今、テーマを動的に変更する必要があります。

私はangular-cli.jsonに2番目のテーマを追加しようとしましたが、期待通りに最初のテーマを上書きしました。

angle-cli.jsonからテーマ宣言を削除し、それぞれ独自のscssスタイルを持つ2つのコンポーネントを持つこともできます.1つはもう一方を上書きし、styleUrlsは唯一の違いです。

また、scssを動的に読み込む他の方法がありますか?

+0

あなたのテーマを 'css class'に取り出せれば' root html要素 'の' class'を '[ngClass]'で切り替えることができます –

+0

Fredrikありがとうございました。 scssファイルを使用する例scssファイルに新しいクラスを追加する必要がありました。私は答えにもっと書きました。 –

答えて

4

回答はChange Material design theme for Angular 2です。 https://github.com/jelbourn/material2-appには良いGITの例があります。

は、だから私は、同じ単一SCSSのテーマファイルを使用しますが、私はそれに新しいテーマのための新しいクラスを追加しました:

.m2app-dark { 
    $dark-primary: md-palette($md-pink, 700, 500, 900); 
    $dark-accent: md-palette($md-blue-grey, A200, A100, A400); 
    $dark-warn: md-palette($md-deep-orange); 
    $dark-theme: md-dark-theme($dark-primary, $dark-accent, $dark-warn); 
    @include angular-material-theme($dark-theme); 
} 

この1つは、HTMLで使用され、アクティブであるかに依存していませんブール値:

<md-sidenav-layout [class.m2app-dark]="isDarkTheme"> 
3

これは、動的なテーマの変更を達成した方法です。

編集可能な例の作業 - 私の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> 

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

+2

これはバージョン5.2.1の私のために働いた – guwere