我正在使用angular2material 构建angular2应用程序.我试图以"正确的方式"设置我的应用程序的背景,但我不知道如何设置.

我发现了一个可以在我的<body>元素上使用的类:mat-app-background,我可以添加它,它给了我一个默认的 colored颜色 (取决于我使用的是亮主题还是暗主题).

我想定义这个背景色来使用我品牌的 colored颜色 ,但我不知道怎么做.

_theming.scss中,它的定义如下:

// Mixin that renders all of the core styles that depend on the theme.
@mixin mat-core-theme($theme) {
  @include mat-ripple-theme($theme);
  @include mat-option-theme($theme);
  @include mat-pseudo-checkbox-theme($theme);

  // Wrapper element that provides the theme background when the
  // user's content isn't inside of a `md-sidenav-container`.
  .mat-app-background {
    $background: map-get($theme, background);
    background-color: mat-color($background, background);
  }
  ...
}

因此,我认为try 在我的自定义主题中添加背景色是有意义的,但我不明白如何做到这一点.

在material 2 theming文档上,它只说:

"在Angular Material中,一个主题是通过组合多个选项板来创建的.特别是,一个主题包括:

  • 主调色板:在所有屏幕和组件中最广泛使用的 colored颜色 .
  • 强调调色板:用于浮动操作按钮和交互元素的 colored颜色 .
  • 警告调色板:用于传达错误状态的 colored颜色 .
  • 前景调色板:文本和图标的 colored颜色 .
  • 背景调色板:用于元素背景的 colored颜色 .

我怎样才能把我的背景添加到主题中,或者以任何其他方式添加到主题中?

推荐答案

如果你想以干净的方式更改整个应用程序的主题背景色,你可以用以下内容覆盖你的主题.

// Set custom background color
$custom-background-color: map_get($mat-blue-grey, 50);

// -or- Can set colour by hex value too
$custom-background-color: #628cc9;

$background: map-get($theme, background);
$background: map_merge($background, (background: $custom-background-color));
$theme: map_merge($theme, (background: $background));

这假设您已经使用mat-light-thememat-dark-theme设置了$theme.当然,你可以用$mat-blue-grey代替你 Select 的彩色 map .

下面是我如何使用它的完整示例.我在一个名为theme.scss的文件中有以下内容,它包含在我的angular.json"styles"条目中:

// Include the common styles for Angular Material. We include this here so that you only
// have to load a single css file for Angular Material in your app.
// Be sure that you only ever include this mixin once!
@include mat-core;

// Define the palettes for your theme using the Material Design palettes available in palette.scss
// (imported above). For each palette, you can optionally specify a default, lighter, and darker
// hue.
$primary: mat-palette($mat-red, 600, 400, 900);
$accent: mat-palette($mat-blue-grey, 500, 200, 700);
$background-color: map_get($mat-blue-grey, 50);

// The warn palette is optional (defaults to red).
$warn: mat-palette($mat-blue);

// Create the theme object (a Sass map containing all of the palettes).
$theme: mat-light-theme($primary, $accent, $warn);

// Insert custom background color
$background: map-get($theme, background);
$background: map_merge($background, (background: $background-color));
$theme: map_merge($theme, (background: $background));

// Include theme styles for core and each component used in your app.
// Alternatively, you can import and @include the theme mixins for each component
// that you are using.
@include angular-material-theme($theme);
@include my-app-theme($theme);

Angular相关问答推荐

根据Angular中的构建来卸载独立组件

HTTP Get请求未发送到提供的URL

Toastr在独立组件上的Angular17实现

未触发HTTP拦截器

向Anguar 17项目添加服务以启动和停止Loader-NGX-UI-Loader

具有多个输入的Angular struct 指令在第一次加载构件时没有值

如何将 Angular 中的导航路由到同一页面并重新加载

Angular 15 Ref 错误:初始化前无法访问组件 A

从具有特定 node 值的现有数组创建新数组

ng test 和 ng e2e 之间的真正区别是什么

如何在Angular Material2中获取活动选项卡

如何在 Angular 2 中获取与 ElementRef 关联的组件的引用

如何使用Angular4通过元素ID设置焦点

等待多个promises完成

如何手动延迟加载模块?

Angular 2:formGroup 需要一个 FormGroup 实例

如何将从后端渲染的参数传递给angular2 bootstrap 方法

来自 chokidar (C:\) 的错误:错误:EBUSY:资源繁忙或锁定,lstat 'C:\DumpStack.log.tmp

Angular2:CoreModule 与 SharedModule

Angular 4 - 获取输入值