简体   繁体   English

如何在Angular2的CSS中使用材质主题颜色?

[英]How do I use a material theme colors in my css in Angular2?

Let's say I need to use the mat-primary color as a background of a div using a css/sass class, how do i reference it? 假设我需要使用css / sass类将mat-primary颜色用作div的背景,我该如何引用它? I thought something like: 我以为是这样的:

@import '~@angular/material/prebuilt-themes/deeppurple-amber.css';

.a-class{
  background-color: $mat-primary;
}

but miserably fails. 但不幸的是失败了。

You have to first import the theme with 您必须先使用

@import '~@angular/material/prebuilt-themes/deeppurple-amber.css';

Then you have to define a mixin like 然后,您必须定义一个混合

@mixin primary-color($theme) {

  $primary: map-get($theme, primary);

  //Then you can use `mat-color` to get the `primary` color
  .a-class {
    background-color: mat-color($primary);
  }
}

Reference Link: Documentation 参考链接: 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM