溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

基于angular?material?theming機制怎么修改mat-toolbar的背景色

發(fā)布時間:2022-10-10 14:26:16 來源:億速云 閱讀:93 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“基于angular material theming機制怎么修改mat-toolbar的背景色”,在日常操作中,相信很多人在基于angular material theming機制怎么修改mat-toolbar的背景色問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”基于angular material theming機制怎么修改mat-toolbar的背景色”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

首先通過 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));

到此,關(guān)于“基于angular material theming機制怎么修改mat-toolbar的背景色”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI