首页IT科技基于 angular material theming 机制修改 mat-toolbar 的背景色(示例详解)

基于 angular material theming 机制修改 mat-toolbar 的背景色(示例详解)

时间2025-05-23 11:57:06分类IT科技浏览5879
导读:最近在学习 angular,记录一下昨天的进展,解决的问题是通过 theme 的配置修改 mat-toolbar 的背景色,避免对色彩的硬编码。...

最近在学习 angular,记录一下昨天的进展,解决的问题是通过 theme 的配置修改 mat-toolbar 的背景色,避免对色彩的硬编码。

首先通过 mat-toolbar (以下统一称为 toolbar)的实现源代码 _toolbar-theme.scss 得知背景色来自 theme 中 background palette 的 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行代码就是我们想使用的背景色

但发现上面的修改对 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 的实现源码 _theming.scss 发现下面的代码

@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 的实现源码 _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 。

最终在之前的代码基础上添加下面的代码解决了问题。

$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 themes color of a specific component

Allow overriding of theme background and foreground colors

到此这篇关于基于 angular material theming 机制修改 mat-toolbar 的背景色的文章就介绍到这了,更多相关angular material theming 修改背景色内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
opencv人脸检测代码(【Android App】人脸识别中借助摄像头和OpenCV实时检测人脸讲解及实战(附源码和演示 超详细)) 面试官:你做过什么有亮点的项目吗英语怎么说(面试官:你做过什么有亮点的项目吗?)