基于?angular?material?theming?機(jī)制修改?mat-toolbar?的背景色(示例詳解)
最近在學(xué)習(xí) angular,記錄一下昨天的進(jìn)展,解決的問題是通過 theme 的配置修改 mat-toolbar 的背景色,避免對色彩的硬編碼。
首先通過 mat-toolbar (以下統(tǒng)一稱為 toolbar)的實(shí)現(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 的實(shí)現(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
進(jìn)一步查看 toolbar 的實(shí)現(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));
在解決這個(gè)問題過程中搜索到的參考材料
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 機(jī)制修改 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-09Angular2實(shí)現(xiàn)組件交互的方法分析
這篇文章主要介紹了Angular2實(shí)現(xiàn)組件交互的方法,結(jié)合實(shí)例形式總結(jié)分析了Angular2中組件交互的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2017-12-12Angular 中使用 FineReport不顯示報(bào)表直接打印預(yù)覽
這篇文章主要介紹了Angular 中使用 FineReport不顯示報(bào)表直接打印預(yù)覽,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08Angular 2父子組件數(shù)據(jù)傳遞之@ViewChild獲取子組件詳解
這篇文章主要給大家介紹了關(guān)于Angular 2父子組件數(shù)據(jù)傳遞之@ViewChild獲取子組件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-07-07AngularJS中的Directive自定義一個(gè)表格
本篇文章給大家介紹在angularjs中自定義一個(gè)有關(guān)表格的directive,涉及到angularjs directive相關(guān)知識,對本文感興趣的朋友一起學(xué)習(xí)吧2016-01-01AngularJS基礎(chǔ) ng-readonly 指令簡單示例
本文主要介紹AngularJS ng-readonly 指令,這里對ng-readonly指令的資料做了整理,有學(xué)習(xí)AngularJS 指令的同學(xué)可以參考下2016-08-08AngularJs的$http發(fā)送POST請求,php無法接收Post的數(shù)據(jù)問題及解決方案
這篇文章主要介紹了AngularJs的$http發(fā)送POST請求,php無法接收Post的數(shù)據(jù)的問題及解決方案,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08