本文小编为大家详细介绍“基于angular material theming机制如何修改mat-toolbar的背景色”,内容详细,步骤清晰,细节处理妥当,希望这篇“基于angular material theming机制如何修改mat-toolbar的背景色”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
首先通过 mat-toolbar (以下统一称为 toolbar)的实现源代码 _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行代码就是我们想使用的背景色
但发现上面的修改对 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));
读到这里,这篇“基于angular material theming机制如何修改mat-toolbar的背景色”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://www.zhuxianfei.com/jishu/js/59360.html