简体   繁体   English

为什么我的自定义 colors 在我的 Angular 材料主题中不起作用?

[英]Why My custom colors are not working in my Angular material theme?

I trying to add more color to my angular material theme, I've added the success color in my style.scss by the map.deep-merge fucntion我试图为我的 angular 材质主题添加更多颜色,我通过 map.deep-merge 功能在我的 style.scss 中添加了成功颜色

// Custom Theming for Angular Material
// For more information: https://material.angular.io/guide/theming
@use '@angular/material' as material;
@use "sass:map";

@include material.core();

$custom-primary: material.define-palette(material.$light-blue-palette);
$custom-accent: material.define-palette(material.$blue-palette, A200, A100, A400);
$custom-warn: material.define-palette(material.$red-palette);
// extra Colors
$custom-success: material.define-palette(material.$green-palette);
$custom-danger: material.define-palette(material.$orange-palette);



$custom-theme: material.define-light-theme((
  color: (
    primary: $custom-primary,
    accent: $custom-accent,
    warn: $custom-warn,
  )
));


$custom-theme: map.deep-merge(
  $custom-theme,(
    success: $custom-success,
    danger: $custom-danger,
    color:(
      success: $custom-success,
      danger: $custom-danger
    )
  )
    );

@include material.all-component-themes($custom-theme);

Everything compiles correctly but when I try to apply the color to a button It looks like this一切都正确编译但是当我尝试将颜色应用于按钮时它看起来像这样在此处输入图像描述

and don't have any idea why.并且不知道为什么。

<button mat-raised-button color="success">Success</button>
<button mat-raised-button color="danger">Danger</button>

Curremtly I'm using "@angular/material": "^13.2.4",目前我正在使用 "@angular/material": "^13.2.4",

I think only one step was missing: Adding .mat-success and .mat-danger CSS classes:我认为只缺少一步:添加.mat-success.mat-danger CSS 类:

.mat-success {
  background-color: material.get-color-from-palette($custom-success, 500);
  color: material.get-color-from-palette($custom-success, 500-contrast);
}

.mat-danger {
  background-color: material.get-color-from-palette($custom-danger, 500);
  color: material.get-color-from-palette($custom-danger, 500-contrast);
}

I also removed the map.deep-merge , which was not necessary in this solution.我还删除了map.deep-merge ,这在此解决方案中不是必需的。


Complete theme.scss file:完整的 theme.scss 文件:

// Custom Theming for Angular Material
// For more information: https://material.angular.io/guide/theming
@use "@angular/material" as material;

@include material.core();

$app-primary: material.define-palette(material.$purple-palette);
$app-accent: material.define-palette(material.$teal-palette, A200);
$app-warn: material.define-palette(material.$red-palette);
// extra Colors
$custom-success: material.define-palette(material.$green-palette);
$custom-danger: material.define-palette(material.$orange-palette);

$custom-theme: material.define-light-theme(
  (
    color: (
      primary: $app-primary,
      accent: $app-accent,
      warn: $app-warn,
    ),
  )
);

@include material.all-component-themes($custom-theme);

.mat-success {
  background-color: material.get-color-from-palette($custom-success, 500);
  color: material.get-color-from-palette($custom-success, 500-contrast);
}

.mat-danger {
  background-color: material.get-color-from-palette($custom-danger, 500);
  color: material.get-color-from-palette($custom-danger, 500-contrast);
}

Github Demo Link (unfortunately Stacklitz has problems with the custom theme) Github Demo链接(可惜Stacklitz自定义主题有问题)

if you used angular-CLI while building the project:如果您在构建项目时使用了 angular-CLI:

as per the official docs -根据官方文档-

If you are using the Angular CLI, support for compiling Sass to CSS is built-in;如果您使用的是 Angular CLI,则内置支持将 Sass 编译为 CSS; you only have to add a new entry to the "styles" list in angular-cli.json pointing to the theme file (eg, custom-theme.scss).您只需在指向主题文件(例如,custom-theme.scss)的 angular-cli.json 中的“样式”列表中添加一个新条目。

So make sure your theme is included in your angular.json and the necessary imports exist in the app.module.ts , and now you can import your theme in every file like this: @import YOUR_FILE_NAME After editing the angular.json file, you will need to run ng-serve again.因此,请确保您的主题包含在您的angular.json中,并且必要的导入存在于app.module.ts中,现在您可以像这样在每个文件中导入您的主题: @import YOUR_FILE_NAME在编辑 angular.json 文件后,您需要运行再次ng-serve


if you are not using angular-CLI:如果你没有使用 angular-CLI:

as per the official docs -根据官方文档-

If you are not using the Angular CLI, you can include a prebuilt theme via a element in your index.html.如果您不使用 Angular CLI,您可以通过 index.html 中的一个元素包含一个预构建的主题。

so first make sure that all the imports are correct所以首先要确保所有的导入都是正确的


if neither helped:如果都没有帮助:

add another import in each of the component files where mat* components are used 在每个使用 mat* 组件的组件文件中添加另一个导入
import { MatButtonModule } from "@angular/material/button";

Remember that when you add that import, your IDE will tell you (by greying it out) that it's not needed and can be removed.请记住,当您添加该导入时,您的 IDE 会告诉您(通过将其变灰)它不需要并且可以删除。


if even that didn't help如果那没有帮助

As per latest angular material documentation you have to import modules from its respective package instead of @angular/material/根据最新的 angular 材料文档,您必须从其各自的 package 而不是@angular/material/导入模块

So change from:所以改变:

import { MatToolbarModule } from '@angular/material/toolbar';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';

to:到:

 import { MatToolbarModule } from '@angular/material/toolbar'; import { MatButtonModule } from '@angular/material/button'; import { MatIconModule } from '@angular/material/icon';

last resort最后一招

try building a minimal reproduction of an app using the same theme you built and post it here or try debuging it yourself (you'll be surprised by how much it can help)尝试使用您构建的相同主题构建应用程序的最小复制品并将其发布在此处或尝试自己调试它(您会惊讶于它有多大帮助)


and in any case after each change, if nothing happens, run ng-serve在任何情况下,每次更改后,如果没有任何反应,请运行ng-serve

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

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