![](/img/trans.png)
[英]How can I use custom material colors outside the node_modules theme file?
[英]How can I use Material Theme colors for custom things?
我搜索了重復使用相同顏色的scss並且遇到了這篇文章 ,我強烈推薦這樣你可以理解整個SCSS文件中的重用值,以使它們更易於維護。
總結一下,您可以通過定義如下內容來定義SCSS變量:
$important-color: rgb(1, 2, 3);
然后重復使用它:
class-1 {
background-color: $important-color;
}
...
class-2 > span {
color: $important-color;
}
因此,您應該做的是在層次結構中較高的SCSS文件中定義其中一個常量(例如app.scss,如果這是一個應用程序范圍的變量)並在組件中使用此變量。 請務必閱讀Aloke的答案,以便進行更深入的分析。
我想你想要一個.scss文件,你可以在其中放置所有自定義顏色變量,並將它們用於通用CSSclass以應用所有應用程序。
然后你需要做這件事。
@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);
// 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;
}
}
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;
}
}
}
此外,您可以在此處查看此代碼的運行示例
對此的回答:問題實際上是我在尋找的東西:它比我想象的容易得多:
.mat-option {
color : mat-color($foreground, text);
}
為什么不使用Angular Material Theming內置函數?
這樣,您只需創建一個自定義調色板,並將其影響為主要/重音/警告color
(或使用預定義的主題)與一點scss(以自定義基色)
然后通過在使用角度材料組件時選擇好的顏色自動完成其余部分:
<button mat-raised-button color="primary">Primary</button>
<button mat-raised-button color="accent">Accent</button>
<button mat-raised-button color="warn">Warn</button>
在您的組件中,如果需要,您還可以非常輕松地使用這些調色板:
button {
background-color: mat-color($accent);
}
如果主題中包含一致的顏色變量名稱,則可以使用這些變量。
但是使用javascript你可以獲得元素的顏色並將其存儲在變量中並在其他地方使用它。
document.getElementById("element").style.backgroundColor
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.