Angular 2/4 How to style angular material design snackbar

前端 未结 7 1903
广开言路
广开言路 2021-02-03 22:22

I am new to Angular2/4 and angular typescript. I want to style the angular material design snackbar for example change the background color from black and font

7条回答
  •  春和景丽
    2021-02-03 22:46

    ::ng-deep support will be dropped, as announced any time soon. Set encapsulation: ViewEncapsulation.None on your message bar component, and you are good to go:

    import { Component, Inject, OnInit, ViewEncapsulation } from '@angular/core';
    import { MAT_SNACK_BAR_DATA } from '@angular/material';
    import { MessageBarType } from './message-bar.model';
    
    @Component({
      selector: 'app-message-bar',
      templateUrl: 'message-bar.component.html',
      styleUrls: ['message-bar.component.scss'],
      encapsulation: ViewEncapsulation.None
    })
    export class MessageBarComponent implements OnInit {
    
      public MessageBarType: typeof MessageBarType = MessageBarType;
    
      constructor(@Inject(MAT_SNACK_BAR_DATA) public data: { text: String, type: MessageBarType }) { }
    
      ngOnInit() {
      }
    }
    

    Then in your scss file:

    @import '~@angular/material/theming';
    @import '~app/theme-defs';
    
    snack-bar-container {
      &.error {
        background-color: mat-color($warn);
      }
    
      &.warning {
        background-color: mat-color($dark-warn);
      }
    
      &.info {
        background-color: mat-color($accent);
      }
    }
    

提交回复
热议问题