Angular Material 2. Switch theme from light to dark on click

后端 未结 1 1984
深忆病人
深忆病人 2020-12-31 23:08

So I have an Angular 2 Material application. And all I wanna do is just switch/toggle theme from dark to light by clicking simple button.

How can I do i

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-31 23:40

    In your menu:

    app.component.html:

    app.component.ts:

    // import statements here
    import {Component} from '@angular/core';
    
    export class AppComponent {
        // Initialize isDarkTheme to false
        isDarkTheme: boolean = false;
        // Your code here
    
        changeTheme(): void {
            if (this.isDarkTheme) {
               this.isDarkTheme = false;
            } else {
               this.isDarkTheme = true;
            }
         }
    }
    

    theme.scss:

    @import '~@angular/material/core/theming/_all-theme';
    
    @include mat-core();
    .dark-theme {
        // Dark theme
        $app-dark-primary: mat-palette($mat-pink, 700);
        $app-dark-accent: mat-palette($mat-blue-grey);
        $app-dark-theme: mat-dark-theme($app-dark-primary, $app-dark-accent);
    
        @include angular-material-theme($app-dark-theme);
    

    }

    0 讨论(0)
提交回复
热议问题