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

 更新时间:2022年10月06日 09:16:33   作者:dudu  
最近在学习 angular,记录一下昨天的进展,解决的问题是通过 theme 的配置修改 mat-toolbar 的背景色,避免对色彩的硬编码,这篇文章主要介绍了基于 angular material theming 机制修改 mat-toolbar 的背景色,需要的朋友可以参考下

最近在学习 angular,记录一下昨天的进展,解决的问题是通过 theme 的配置修改 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));

在解决这个问题过程中搜索到的参考材料

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

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

相关文章

  • AngularJS深入探讨scope,继承结构,事件系统和生命周期

    AngularJS深入探讨scope,继承结构,事件系统和生命周期

    这篇文章主要介绍了AngularJS的scope,继承结构,事件系统和生命周期,较为详细的分析了scope的作用域、层次结构、继承及生命周期相关概念与使用技巧,需要的朋友可以参考下
    2016-11-11
  • 深入浅析AngularJs模版与v-bind

    深入浅析AngularJs模版与v-bind

    v-bind与{{}}都可以对数据进行绑定,接下来通过本文给大家介绍AngularJs模版与v-bind的相关知识,感兴趣的朋友一起看看吧
    2018-07-07
  • Angularjs过滤器实现动态搜索与排序功能示例

    Angularjs过滤器实现动态搜索与排序功能示例

    这篇文章主要介绍了Angularjs过滤器实现动态搜索与排序功能,涉及AngularJS过滤器相关搜索、查询、排序操作技巧,需要的朋友可以参考下
    2017-12-12
  • AngularJS 使用$sce控制代码安全检查

    AngularJS 使用$sce控制代码安全检查

    SCE,即strict contextual escaping,我的理解是 严格的上下文隔离 ...翻译的可能不准确,但是通过字面理解,应该是angularjs严格的控制上下文访问,通过本文给大家介绍AngularJS 使用$sce控制代码安全检查,对angularjs sce相关知识感兴趣的朋友一起学习吧
    2016-01-01
  • AngularJS 获取ng-repeat动态生成的ng-model值实例详解

    AngularJS 获取ng-repeat动态生成的ng-model值实例详解

    这篇文章主要介绍了AngularJS 获取ng-repeat动态生成的ng-model值实例详解的相关资料,这里提供实例代码及实现效果图,需要的朋友可以参考下
    2016-11-11
  • 浅谈angularJS2中的界面跳转方法

    浅谈angularJS2中的界面跳转方法

    今天小编就为大家分享一篇浅谈angularJS2中的界面跳转方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • AngularJS中的过滤器使用详解

    AngularJS中的过滤器使用详解

    这篇文章主要介绍了AngularJS中的过滤器使用详解,过滤器是AngularJS中处理文本的一个非常有用的功能,需要的朋友可以参考下
    2015-06-06
  • Angular中的Promise对象($q介绍)

    Angular中的Promise对象($q介绍)

    这篇文章主要介绍了Angular中的Promise对象($q介绍),本文讲解了Promise模式、Q Promise的基本用法、AngularJs中的$q.defferd等内容,需要的朋友可以参考下
    2015-03-03
  • angularjs基础教程

    angularjs基础教程

    AngularJS是为了克服HTML在构建应用上的不足而设计的。HTML是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了。所以我做了一些工作(你也可以觉得是小花招)来让浏览器做我想要的事。
    2014-12-12
  • Angular 结合 dygraphs 实现 annotation功能

    Angular 结合 dygraphs 实现 annotation功能

    这篇文章主要介绍了Angular 结合 dygraphs 实现 annotation,本文,我们直接结合 Angular 来演示,如何通过 dygraphs 实现折线图上的 annotation 的功能,需要的朋友可以参考下
    2022-08-08

最新评论