我认为您需要一个 .scss 文件,您可以在其中放置所有自定义颜色变量并将它们用于通用 CSSclass 以应用于您的所有应用程序。
那么你需要做这些事情。
- 使用包含当前材质主题/动态主题 scss 的 theme.scss 文件
@import '~@angular/material/theming';
@import './mytheme-sidemenu.scss';
// Primary theme
@include mat-core();
$mytheme-app-primary: mat-palette($mat-deep-purple, 700, 600);
$mytheme-app-accent: mat-palette($mat-red, A200, 900, A100);
$mytheme-app-warn: mat-palette($mat-deep-orange);
$mytheme-app-theme: mat-light-theme($mytheme-app-primary, $mytheme-app-accent, $mytheme-app-warn);
@include angular-material-theme($mytheme-app-theme);
// Secondary Theme
.mytheme-alt-theme {
$mytheme-alt-primary: mat-palette($mat-teal, 500);
$mytheme-alt-accent: mat-palette($mat-orange, 500);
$mytheme-alt-warn: mat-palette($mat-red);
$mytheme-alt-theme: mat-light-theme($mytheme-alt-primary, $mytheme-alt-accent, $mytheme-alt-warn);
@include angular-material-theme($mytheme-alt-theme);
}
.mytheme-another-alt-theme{
$mytheme-another-alt-primary: mat-palette($mat-blue, 500);
$mytheme-another-alt-accent: mat-palette($mat-yellow, 500);
$mytheme-another-alt-warn: mat-palette($mat-green);
$mytheme-another-alt-theme: mat-light-theme($mytheme-another-alt-primary, $mytheme-another-alt-accent, $mytheme-another-alt-warn);
@include angular-material-theme($mytheme-another-alt-theme);
}
// Using the $theme variable from the pre-built theme you can call the theming function
@include mytheme-sidemenu($mytheme-app-theme);
- 使用单独的 scss 文件来保存您的自定义 css 类
// Import all the tools needed to customize the theme and extract parts of it
@import "~@angular/material/theming";
// Define a mixin that accepts a theme and outputs the color styles for the component.
@mixin mytheme-sidemenu($theme) {
// Extract whichever individual palettes you need from the theme.
$primary: map-get($theme, primary);
$accent: map-get(
$theme,
accent
); // Use mat-color to extract individual colors from a palette as necessary.
.col-primary {
color: mat-color($primary, 500) !important;
}
.col-accent {
color: mat-color($accent, 300) !important;
}
}
- 现在您可以在应用程序上使用您的自定义 css 类,并且不要忘记将 scss 文件链接放在您的 angular.json/angular-cli.json 中
You can also use this classes too:
.col-primary {
& .hue {
&-50 {
color: mat-color($primary, 50) !important;
}
&-100 {
color: mat-color($primary, 100) !important;
}
.
.
.
&-800 {
color: mat-color($primary, 800) !important;
}
}
}
另外,你可以看到这段代码的运行示例here