Angular 2 + Material : Unable to use map-get with 2nd parameter "text", "card"等

标签 angular sass angular-material

使用 Angular 2 (4) + Material,我需要自定义组件的主题。我首先尝试使用内置调色板,但不断收到“错误:未定义”。 仅当将“primary”、“accent”或“warn”作为第二个参数传递给map-get 时,它才有效:

styles.scss

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

@import 'app/app.component.scss-theme';

$default-theme-primary: mat-palette($mat-light-green);
$default-theme-accent:  mat-palette($mat-indigo);
$default-theme-warn:    mat-palette($mat-red);
$default-theme: mat-light-theme($default-theme-primary, $default-theme-accent, $default-theme-warn);

@include angular-material-theme($default-theme);
@include app-component-theme($default-theme);

app.component.scss-theme.scss

@import '~@angular/material/theming';

@mixin app-component-theme($theme){

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

  $text: map-get($theme, text);
  $card: map-get($theme, card);
  $lighter: map-get($theme, lighter);
  $darker: map-get($theme, darker);

  .test{
    font-size: 5em;
    color: mat-color($primary)!important; // works
    // color: mat-color($darker)!important; // fails
    // color: mat-color($lighter)!important; // fails
    // color: mat-color($text)!important; // fails
    // color: mat-color($card)!important; // fails
  }
}

我依靠以下帖子/网址来找到第二个参数接受的可能值的方法,但可以知道出了什么问题。

stack overflow discussion _themes.scss custom theming

最佳答案

好的,发现我的错误:

$text: map-get($theme, text);
  $card: map-get($theme, card);
  $lighter: map-get($theme, lighter);
  $darker: map-get($theme, darker);

  .test{
    font-size: 5em;
    color: mat-color($darker)!important; // fails because $darker isn't called on foreground
    }

解决方案:“$foreground”和“$background” map 上存在文本、卡片等:

@mixin app-component-theme($theme){

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

  $foreground: map-get($theme, foreground);
  $background: map-get($theme, background);

  $text: map-get($foreground, text);
  $card: map-get($background, card);

  .crap{
    font-size: 5em;
    color: $text;
    background-color: $card;
  }
}

关于Angular 2 + Material : Unable to use map-get with 2nd parameter "text", "card"等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46648928/

相关文章:

checkbox - Angular2 有没有办法使用复选框的双向绑定(bind)?

asp.net - 在 IIS 中部署 Angular 6 应用程序后,我收到一个空白页面,没有错误

angular - 我可以用 angular/typescript 给导入的库起一个别名吗?

css - 如何使用 Compass background-with-css2-fallback mixin?

html - 垂直 MD 工具栏

Angular Material - 如何自定义所选选项的样式

angular - 在Angular 5中等待子组件初始化

css - 是否可以制作构成 SVG 图像的笔画的渐变颜色

sass - Webpack 文件加载器重复的公共(public)路径

javascript - 使用 Angular 指令在模板中绘制对象