Я нашел свое решение. Я публикую это здесь, так что если кому-то еще нужно подобное поведение в своем угловом приложении, он может получить его следующим образом.
Я создал миксин для нижнего колонтитула
@mixin footer-theme($theme) {
$primary: map-get($theme, primary);
$accent: map-get($theme, accent);
$warn: map-get($theme, warn);
$background: map-get($theme, background);
$foreground: map-get($theme, foreground);
app-footer {
.footer-primary {
background-color: mat-color($primary);
}
.footer-accent {
background-color: mat-color($accent);
}
}
}
app-footer
- это селектор компонента, а .footer-*
- это класс компонента нижнего колонтитула, к которому я хочу применить основной / акцентный цвет фона.
В компоненте нижнего колонтитула color
является входом . Нам нужно передать значение цвета при встраивании компонента нижнего колонтитула следующим образом.
Для основного цвета фона:
<app-footer color="primary"></app-footer>
Для цвета фона акцента:
<app-footer color="accent"></app-footer>
В файле HTML нижнего колонтитула:
<div [ngClass]="{'footer-primary': color === 'primary', 'footer-accent': color === 'accent'}">
/*footer code here...*/
</div>
Затем я включил миксин нижнего колонтитула в файл app_theme.scss , который имеетнесколько тем
@import '~@angular/material/theming';
@import './app/shared/footer/footer-theme';
@include mat-core();
$primary1: mat-palette($mat-teal, 500);
$accent1: mat-palette($mat-green, A200, A100, A400);
$warn1: mat-palette($mat-red);
$theme1: mat-light-theme($primary1, $accent1, $warn1);
@include angular-material-theme($theme1);
@include footer-theme($theme1);
// SECOND THEME
$primary2: mat-palette($mat-indigo, 500);
$accent2: mat-palette($mat-blue, A200, A100, A400);
$warn2: mat-palette($mat-red);
$theme2: mat-light-theme($primary2, $accent2, $warn2);
.second-theme {
@include angular-material-theme($theme2);
@include footer-theme($theme2);
}
В app.component.html
<div [ngClass]="{'second-theme' : isSecondTheme}">
<div class="mat-app-background">
<router-outlet></router-outlet>
</div>
</div>
Здесь isSecondTheme
- логическое значение, когда true
вторая тема применяется, в противном случае тема по умолчанию. Просто измените логическое значение во время выполнения.
Вы можете изменить его, чтобы иметь более двух тем.