基于?angular?material?theming?機制修改?mat-toolbar?的背景色(示例詳解)
最近在學習 angular,記錄一下昨天的進展,解決的問題是通過 theme 的配置修改 mat-toolbar 的背景色,避免對色彩的硬編碼。
首先通過 mat-toolbar (以下統(tǒng)一稱為 toolbar)的實現(xiàn)源代碼 _toolbar-theme.scss 得知背景色來自 theme 中 background palette 的 app-bar。
background: theming.get-color-from-palette($background, app-bar);
于是通過下面的 scss 代碼修改 app-bar 的顏色值
$app-bar-background: map-get(mat.$grey-palette, 900); $background-palette: map-get($theme, background); $background-palette: map-merge($background-palette, (app-bar: $app-bar-background)); $theme: map-merge($theme, (background: $background-palette));
注:第1行代碼就是我們想使用的背景色
但發(fā)現(xiàn)上面的修改對 toolbar 沒有起作用,而通過下面的代碼可以拿到修改后的背景色
$background-palette: map-get($theme, background); background-color: mat.get-color-from-palette($background-palette, app-bar);
看來 mat-toolbar 不是通過 theme 的 background 獲取背景色的。
查看的 define-light-theme 的實現(xiàn)源碼 _theming.scss 發(fā)現(xiàn)下面的代碼
@if $accent != null {
@warn $_legacy-theme-warning;
@return private-create-backwards-compatibility-theme(_mat-validate-theme((
_is-legacy-theme: true,
color: _mat-create-light-color-config($primary, $accent, $warn),
)));
}由此猜測 toolbar 可能是 legacy theme
進一步查看 toolbar 的實現(xiàn)源碼 _toolbar-theme.scss
@mixin theme($theme-or-color-config) {
$theme: theming.private-legacy-get-theme($theme-or-color-config);
@include theming.private-check-duplicate-theme-styles($theme, 'mat-toolbar') {
$color: theming.get-color-config($theme);
$density: theming.get-density-config($theme);
$typography: theming.get-typography-config($theme);
// ...
}
}的確是 legacy theme 。
最終在之前的代碼基礎(chǔ)上添加下面的代碼解決了問題。
$color-palette: map-get($theme, color); $color-background-palette: map-get($color-palette, background); $color-background-palette: map-merge($color-background-palette, (app-bar: $app-bar-background)); $color-palette: map-merge($color-palette, (background: $color-background-palette)); $theme: map-merge($theme, (color: $color-palette));
在解決這個問題過程中搜索到的參考材料
Changing the background color in an Angular Material theme
How to get the current angular theme's color of a specific component
Allow overriding of theme background and foreground colors
到此這篇關(guān)于基于 angular material theming 機制修改 mat-toolbar 的背景色的文章就介紹到這了,更多相關(guān)angular material theming 修改背景色內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
angular route中使用resolve在uglify壓縮后問題解決
這篇文章主要介紹了angular route中使用resolve在uglify壓縮后問題解決的相關(guān)資料,需要的朋友可以參考下2016-09-09
Angular 中使用 FineReport不顯示報表直接打印預(yù)覽
這篇文章主要介紹了Angular 中使用 FineReport不顯示報表直接打印預(yù)覽,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08
Angular 2父子組件數(shù)據(jù)傳遞之@ViewChild獲取子組件詳解
這篇文章主要給大家介紹了關(guān)于Angular 2父子組件數(shù)據(jù)傳遞之@ViewChild獲取子組件的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家具有一定參考學習價值,需要的朋友們下面來一起看看吧。2017-07-07
AngularJS基礎(chǔ) ng-readonly 指令簡單示例
本文主要介紹AngularJS ng-readonly 指令,這里對ng-readonly指令的資料做了整理,有學習AngularJS 指令的同學可以參考下2016-08-08
AngularJs的$http發(fā)送POST請求,php無法接收Post的數(shù)據(jù)問題及解決方案
這篇文章主要介紹了AngularJs的$http發(fā)送POST請求,php無法接收Post的數(shù)據(jù)的問題及解決方案,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08

