2016-04-29 88 views
5

我正嘗試在我的Angular 2應用程序中使用Angular Material 2更改Angular 2的材質設計主題

我沒有找到如何改變全球物質主題(primaryColor等)。 我知道Angular Material 2仍然處於alpha階段,但目前有什麼方法可以做到這一點?

+0

您是否閱讀過角材料1的文檔?也許它是相似的。如果還沒有2的文檔,請嘗試查看源代碼以找出它。或者在他們的github項目上提出問題。 –

回答

3

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

@ samio80的風格正在寫在心中的主題化,但我們並沒有對主題化還沒有準備好部署策略。作爲一種解決方法,您可以直接拉動源代碼並通過修改_default-theme.scss和創建npm包(通過腳本stage-release.sh)來自定義主題。

請記住,我們仍然處於alpha過程的早期階段,因此API或行爲可以在不同版本之間更改。

+0

好的,謝謝:)你有關於beta版發佈日期的想法嗎? – quen2404

+0

不,谷歌沒有提供任何時間表=>「什麼時候準備就緒」 –

+0

@GünterZöchbauer我所做的是從主題複製樣式並將其添加到組件的樣式表中是否正確使用主題? – blackHawk

1

這裏的動態角材料主題實現的示例如角5.1和角材5.0。

編輯工作的例子 - https://stackblitz.com/edit/dynamic-material-theming

在theme.scss文件,包括默認的主題(注意它下一個類名沒有保持 - 這是這麼角將用它作爲默認設置),然後光明和黑暗的主題。

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

在app.component文件,包括從@角蛋白/ cdk /重疊OverlayContainer。你可以在這裏找到Angular的文檔https://material.angular.io/guide/theming;儘管他們的實施有些不同。請注意,我也必須在app.module中包含OverlayModule作爲導入。

在我的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 {} 

最後,調用從視圖中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> 

您可以考慮使用可觀察到的,這樣的功能會更有活力。

+0

雖然這可能會回答這個問題, [這將是更可取的](http://meta.stackoverflow.com/q/8259)在這裏包括 的答案的基本部分,並提供鏈接供參考。 –

+0

對不起,我在帖子中提供了更多細節。 –

+0

請不要發佈相同的答案多個問題。發佈一個很好的答案,然後投票/標記以重複關閉其他問題。如果問題不重複,*定製您的問題答案。* –