【问题标题】:angular material using theme colors使用主题颜色的角材质
【发布时间】:2018-09-14 18:11:31
【问题描述】:

我正在使用 angular 5 材质,并创建了一个 theme.scss,如下所示 theme.scss

 @import '~@angular/material/theming';
    @include mat-core();

    $custom-primary: mat-palette($mat-deep-purple,600);
    $custom-accent:  mat-palette($mat-lime, 100);
    $custom-warn:    mat-palette($mat-red);

    $custom-theme: mat-light-theme($custom-primary, $custom-accent, $custom-warn);

    @include angular-material-theme($custom-theme);

    // ALTERNATIVE THEME

    $alt-primary: mat-palette($mat-yellow);
    $alt-accent:  mat-palette($mat-grey, 200);

    $alt-theme: mat-dark-theme($alt-primary, $alt-accent);

    .alternative {
        @include angular-material-theme($alt-theme);
    }

我有我的默认样式.scss 如下 style.scss

   @import "~@angular/material/prebuilt-themes/indigo-pink.css";

        .fa-icon-nav {
            color: #507889;
            font-size: x-large;
        }

颜色当前在 fa-icon-nav 中硬编码。我希望它使用当前选定主题的原色。如果可能的话,请告知这将如何工作?很高兴听到这是否是完全错误的方法以及应该如何做。

【问题讨论】:

    标签: css angular sass themes angular-material2


    【解决方案1】:

    编辑:

    ADDED ONLINE DEMO


    我正在做一个使用 Material 2 主题的项目,我使用了这种方法,我使用类名并全局添加颜色类。

    这就是我所做的:

    文件名:mytheme-sidemenu.scss:

    // Import all the tools needed to customize the theme and extract parts of it
    @import "~@angular/material/theming";
    // Define a mixin that accepts a theme and outputs the color styles for the component.
    @mixin mytheme-sidemenu($theme) {
      // Extract whichever individual palettes you need from the theme.
      $primary: map-get($theme, primary);
      $accent: map-get(
        $theme,
        accent
      ); // Use mat-color to extract individual colors from a palette as necessary.
    
      .col-primary {
        color: mat-color($primary, 500) !important;
      }
      .col-accent {
        color: mat-color($accent, 300) !important;
      }
    }
    

    这是我的主主题文件:mytheme-theme.scss:

    @import '~@angular/material/theming';
    @import './variables/helper.scss';
    @import './variables/spacemanager.scss';
    @import './mytheme-sidemenu.scss';
    
    // Primary theme
    @include mat-core();
    $mytheme-app-primary: mat-palette($mat-light-blue, 700, 600);
    $mytheme-app-accent: mat-palette($mat-pink, A200, 900, A100);
    $mytheme-app-warn: mat-palette($mat-deep-orange);
    $mytheme-app-theme: mat-light-theme($mytheme-app-primary, $mytheme-app-accent, $mytheme-app-warn);
    @include angular-material-theme($mytheme-app-theme);
    // Secondary Theme
    .mytheme-alt-theme {
        $mytheme-alt-primary: mat-palette($mat-blue-grey, 500);
        $mytheme-alt-accent: mat-palette($mat-pink, 500);
        $mytheme-alt-warn: mat-palette($mat-deep-orange);
        $mytheme-alt-theme: mat-light-theme($mytheme-alt-primary, $mytheme-alt-accent, $mytheme-alt-warn);
        @include angular-material-theme($mytheme-alt-theme);
    }
    // Using the $theme variable from the pre-built theme you can call the theming function
    @include mytheme-sidemenu($mytheme-app-theme);
    

    并在 app.module.ts 中更新:

    export class AppModule {
      constructor(
        @Inject(OverlayContainer) private overlayContainer: OverlayContainer
      ) {
        this.overlayContainer
          .getContainerElement()
          .classList.add("mytheme-alt-theme"); // this for double theme add to the root css class
      }
    }
    

    这已经被问过了,我只是从here复制粘贴我的答案

    编辑:

    根据您的需要,您可以做到这一点:

    我的默认 style.scss 如下 style.scss,您需要将其更改为 _theme-color.scss

     .fa-icon-nav {
      color: mat-color($primary, 500) !important; // 500, 600 , 700 check material color pallet for more info
      // You can use this too
      // color: mat-color($alt-primary, 500) !important;
      font-size: x-large;
    }
    

    或者你可以使用here的这个材质颜色库

    【讨论】:

    • 感谢您的回复。但我不认为这对于我只是找到当前主题原色或强调色的简单要求是必需的。有什么建议可以修改我现有的 css 以提取所需的信息?
    • @Vk 恐怕这种方式可能是唯一的方法。否则,您可能只需要硬编码您的值。
    • 可能是我无法理解 Aloke。您可以修改您的答案以适应我的示例中的工作方式吗?
    • 你的编辑听起来我可以使用 $primary 或 $alt-primary。但是,我需要能够根据我选择的主题切换到两者之一。如果我将硬编码主题变量,那么它不会改变。有意义吗?
    • 不明白我的默认styles.scss如下style.scss,你需要更改为_theme-color.scss,因为它是需要的
    猜你喜欢
    • 2018-09-25
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    相关资源
    最近更新 更多