基于?angular?material?theming?機(jī)制修改?mat-toolbar?的背景色(示例詳解)
最近在學(xué)習(xí) angular,記錄一下昨天的進(jìn)展,解決的問題是通過 theme 的配置修改 mat-toolbar 的背景色,避免對(duì)色彩的硬編碼。
首先通過 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)上面的修改對(duì) 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),
)));
}由此猜測(cè) 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ANGULARJS中用NG-BIND指令實(shí)現(xiàn)單向綁定的例子
這篇文章主要介紹了ANGULARJS中用NG-BIND指令實(shí)現(xiàn)單向綁定的例子,本文簡(jiǎn)單介紹AngularJS框架后,用一個(gè)實(shí)例演示{{}}插值法和ng-bind指令的使用,需要的朋友可以參考下2014-12-12
AngularJS實(shí)現(xiàn)樹形結(jié)構(gòu)(ztree)菜單示例代碼
這篇文章運(yùn)用示例代碼給大家詳細(xì)介紹了利用AngularJS如何實(shí)現(xiàn)樹形結(jié)構(gòu)(ztree)菜單,文中僅用了幾行AngularJS代碼就是了這個(gè)功能,對(duì)大家日常開發(fā)很有幫助,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-09-09
ng2學(xué)習(xí)筆記之bootstrap中的component使用教程
現(xiàn)在angular2已經(jīng)除了集成的angular-cli,建議大家可以基于這個(gè)來快速開發(fā)ng2的項(xiàng)目,不用自己再搭建環(huán)境。接下來通過本文給大家介紹 bootstrap中的component使用教程,需要的朋友可以參考下2017-03-03
對(duì)Angular.js Controller如何進(jìn)行單元測(cè)試
這篇文章主要給大家介紹了如何對(duì)Angular Controller進(jìn)行單頁(yè)測(cè)試。如果你不太了解angular也沒關(guān)系,本文也會(huì)提及關(guān)于Angular的一些知識(shí)。文中通過示例代碼介紹的很詳細(xì),詳細(xì)對(duì)大家的理解和學(xué)習(xí)很有幫助,下面來一起看看吧。2016-10-10
Angular中實(shí)現(xiàn)樹形結(jié)構(gòu)視圖實(shí)例代碼
近兩年當(dāng)中使用Angular開發(fā)過很多項(xiàng)目,其中也涉及到一些樹形結(jié)構(gòu)視圖的顯示,最近的在項(xiàng)目中封裝了大量的組件,一些組件也有樹形結(jié)構(gòu)的展示,所以寫出來總結(jié)一下。2017-05-05
如何處理Angular?錯(cuò)誤消息ERROR?Error?NullInjectorError?No?provid
這篇文章主要介紹了如何處理Angular?錯(cuò)誤消息ERROR?Error?NullInjectorError?No?provider?for?XX2023-07-07
angularjs實(shí)現(xiàn)搜索的關(guān)鍵字在正文中高亮出來
這篇文章主要介紹了angularjs實(shí)現(xiàn)搜索的關(guān)鍵字在正文中高亮出來,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
angularjs ui-router中路由的二級(jí)嵌套
本篇文章主要介紹了angularjs ui-router中路由的二級(jí)嵌套,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03

