首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角度材质主题不会改变mat-dialog强调色

角度材质主题不会改变mat-dialog强调色
EN

Stack Overflow用户
提问于 2021-01-18 01:05:07
回答 1查看 885关注 0票数 1

我需要改变我的应用程序的颜色主题根据一个参数,这应该是一个非常简单的操作。在我的应用程序中,我使用了熔丝角度材质主题。当我尝试从主要主题切换到次要主题时,对话框组件的强调色不会改变,而其他组件(例如导航栏)会改变。

_theme.scss

代码语言:javascript
复制
@import '~@angular/material/theming';
@import './component-themes';

$primary: mat-palette($mat-fusedark);
$accent: mat-palette($mat-light-blue, 600, 400, 700);
$warn: mat-palette($mat-red);
$white: mat-palette($mat-white);
$black: mat-palette($mat-black);

$first-theme: mat-light-theme($primary, $accent, $warn);

$background: map-get($first-theme, background);
$foreground: map-get($first-theme, foreground);

@include angular-material-theme($first-theme);
@include component-themes($first-theme);

$second-primary: mat-palette($mat-fusedark);
$second-accent: mat-palette($mat-green, 600, 400, 700);
$second-warn: mat-palette($mat-red);

$second-theme: mat-light-theme($second-primary, $second-accent, $second-warn);

.second-theme {
  @include angular-material-theme($second-theme);
  @include component-themes($second-theme);
}

component-theme.scss

代码语言:javascript
复制
@import "../partials/navigation";
@import "../partials/dialog";

@mixin component-themes($theme) {
  @include navigation-theme($theme);
  @include dialog-theme($theme);
}

_dialog.scss

代码语言:javascript
复制
@import '~@angular/material/theming';

    @mixin dialog-theme($theme) {
      $accent: map-get($theme, accent);
    
      .dialog-wrapper {
        .mat-dialog-container {
          padding: 0;
          overflow: hidden;
        }
    
        .mat-dialog-title {
          display: flex;
          flex: 1 1 auto;
          justify-content: space-between;
          margin: 0;
          padding: 24px;
    
        }
    
        .mat-toolbar {
          background-color: mat-color($accent) !important;
        }
    
        .mat-dialog-content {
          padding: 16px 32px 0px;
          margin: 0;
        }
    
        .mat-dialog-actions {
          display: flex;
          flex: 1 1 auto;
          margin: 1px 0 0 0;
          padding: 0px 32px 16px;
        }
    
      }
    }

如果我在_dialog.scss中更改了值

代码语言:javascript
复制
background-color: mat-color($accent) !important;

转到

代码语言:javascript
复制
background-color: green !important;

它工作正常。看起来mat-color($accent)不起作用,但只适用于该组件的scss。

EN

回答 1

Stack Overflow用户

发布于 2021-03-08 19:39:34

对于谁遇到这个问题,我终于找到了解决方案。我无法更改某些组件的主题,因为它们是嵌套的,覆盖容器会阻止主题传播。为了修复它,我导入了overlayContainer,并在ngOnInit方法中将主题类添加到overlayContainer中。

代码语言:javascript
复制
import {OverlayContainer} from '@angular/cdk/overlay';

constructor(
    private overlayContainer: OverlayContainer
  ) {}

ngOnInit() {
    if (this.data.theme === 'second-theme') {
          this.overlayContainer.getContainerElement().classList.add(this.data.theme);
        }
}

非常简单的解决方案当您知道问题是什么时,最困难的部分是找出它:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65763544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档