【问题标题】:Can you use mat-color with Angulars theme?你可以在 Angulars 主题中使用 mat-color 吗?
【发布时间】:2020-12-25 05:41:49
【问题描述】:

我在我的 Angular 9 应用程序中使用 Angular 主题,我有一个实例,我想使用 mat-color 更亮和更暗(分别用于颜色和背景色),我之前已经使用自定义主题,但从不使用 Angular 主题。

我已经添加了

background-color: mat-color($color-error, lighter);
color: mat-color($color-error, darker);

到我的 css,其中 $color-error 是我创建的变量。颜色没有传到课堂上。

我可以用浅色或深色搭配垫子颜色吗?

谢谢

【问题讨论】:

    标签: angular typescript sass


    【解决方案1】:

    这里是an example I created on stackblitz,关于如何创建具有角度的主题并根据调色板颜色定义较浅和较深的颜色

    这是重要的代码:

    主题.scss:

    @import '~@angular/material/theming';
    
    @include mat-core();
    
    $custom-primary: mat-palette($mat-teal);
    $custom-accent:  mat-palette($mat-pink, A200, A100, A400);
    $custom-warn:    mat-palette($mat-red);
    $custom-theme:   mat-light-theme($custom-primary, $custom-accent, $custom-warn);
    
    @include angular-material-theme($custom-theme);
    

    在这个文件中,我创建了一个自定义主题并定义了一个浅色主题。

    变量.scss:

    @import './theme.scss';
    
    $primary: mat-color($custom-primary);
    $primary-lighter: mat-color($custom-primary, lighter);
    $primary-darker: mat-color($custom-primary, darker);
    

    在这里,我根据上面主题中定义的原色创建了 3 个包含原色、浅色和深色的变量。

    请注意,较浅和较深的颜色必须基于调色板中的颜色。您不能只选择一种随机颜色(即使它是有角度的颜色。对于此处的红色,您必须使用$custom-warn)。如果您仍然需要从调色板中调亮或调暗颜色,仍然有 scss lighten 和 darken 混合允许您这样做:

    lighten($myColor, 30%),darken 相同。

    要使这个主题和变量起作用,您需要在styles.scss 中导入theme.scss,并且无论您想在何处使用自定义变量,都必须导入variables.scss,仅此而已。

    【讨论】:

      猜你喜欢
      • 2012-06-21
      • 1970-01-01
      • 2020-01-29
      • 2016-12-01
      • 2010-09-23
      • 1970-01-01
      • 2014-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多