简体   繁体   English

Angular 材质切换主题时切换A颜色

[英]Angular Material Switch A Color When Switching Theme

I have implemented Angular Material Dark and Light theme.我已经实现了 Angular Material Dark and Light 主题。 With this I can change the colors based on the theme using for example mat-color($primary) .有了这个,我可以使用例如mat-color($primary)根据主题更改 colors 。 This works fine but now I need to use a different color altogether.这很好用,但现在我需要完全使用不同的颜色。 So instead of the primary color with hue 200 in light theme: mat-color($primary, 200) , I want to be able to use primary color with an 800 hue in dark theme mat-color($primary, 800) .因此,我希望能够在深色主题mat-color($primary, 800)中使用具有 800 色调的原色,而不是浅色主题中色调为 200 的原色: mat-color($primary, 200) ) 。

data-table-theme.scss数据表-theme.scss

@mixin data-table-color($color-config) {
  $primary: map-get($color-config, primary);

  .data-table {
    background-color: mat-color($primary, 200);
  }

}

data-table.html数据表.html

<div class="page-container mat-app-background"
     [ngClass]="(isDarkTheme$ | async) ? 'theme-dark' : 'theme-default'">

  <div class="data-table"></div>

</div>

I tried adding theme-dark selector in custom component style but that didn't work.我尝试在自定义组件样式中添加theme-dark选择器,但这不起作用。

@mixin data-table-color($color-config) {
  $primary: map-get($color-config, primary);

  .theme-default .data-table {
    background-color: mat-color($primary, 200);
  }

  .theme-dark .data-table {
    background-color: mat-color($primary, 800);
  }

}

I look forward to hearing your solutions.我期待听到您的解决方案。

Edit编辑

The solution I tried doesn't work because angular material generates the css at compile time.我尝试的解决方案不起作用,因为 angular 材料在编译时生成 css。 So it will then create:因此它将创建:

.theme-default .data-table {
  background-color: #c5cae9;
}

.theme-dark .data-table {
  background-color: #1a237e;
}

.theme-dark .theme-dark .data-table {
  background-color: #ff6f00;
}

.theme-dark .theme-default .data-table {
  background-color: #ffecb3;
}

This causes it to change hue but the color used is the light primary color.这会导致它改变色调,但使用的颜色是浅色原色。

I have uploaded a test project to Stackblitz, which unfortunately doesn't compile some of the angular material dependencies.我已经将一个测试项目上传到 Stackblitz,不幸的是它没有编译一些 angular 材料依赖项。 But if you run it locally you will see what I mean.但是如果你在本地运行它,你会明白我的意思。

https://stackblitz.com/github/kdrpt/angular-test-project https://stackblitz.com/github/kdrpt/angular-test-project

Have you added the include statement for you mixin in your scss file?您是否在 scss 文件中为您的 mixin 添加了 include 语句?

@include data-table-color($color-config);

Just add the above statement in your scss file (where you have defined your @mixin data-table-color) and pass the $color-config that you have created.只需将上述语句添加到您的 scss 文件(您在其中定义了 @mixin 数据表颜色)并传递您创建的$color-config即可。 It is working fine for me.它对我来说很好。

Looking in your stackblitz example, it seems you forgot to import your app.theme.scss in the root styles.scss .查看您的 stackblitz 示例,您似乎忘记在根styles.scss app.theme.scss

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

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

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