【问题标题】:Angular Material 2. Switch theme from light to dark on clickAngular Material 2. 单击时将主题从浅色切换到深色
【发布时间】:2017-07-17 19:51:38
【问题描述】:

所以我有一个 Angular 2 Material 应用程序。 我想做的只是通过单击简单的button 将主题从黑暗切换到明亮。

我该怎么做?

【问题讨论】:

    标签: angular typescript material-design angular-material2


    【解决方案1】:

    在你的menu:

    app.component.html

    <div [class.dark-theme]="isDarkTheme">
        <!--Your application content here-->
        <md-menu #more="mdMenu">
            <!--Your content here-->
            <button md-menu-item (click)="changeTheme()">
                Change Theme
            </button>
        </md-menu>
    </div>
    

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

    }

    【讨论】:

    • 我还应该在我的 angular-cli.json 文件中添加sometheme.scss 吗?
    • 是的,你应该添加它。
    猜你喜欢
    • 1970-01-01
    • 2019-08-20
    • 2021-03-10
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 2022-08-12
    相关资源
    最近更新 更多