Angular 5 Material Snackbar panelClass config

后端 未结 4 1105
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-04 19:16

I am trying to add a panelClass config to the Angular Material Snackbar.

I wrote the following code, by following documentations from the official websites.

相关标签:
4条回答
  • 2021-01-04 19:37

    panelClass is defined as

    panelClass: string | string[]

    Extra CSS classes to be added to the snack bar container.

    It is used to add a class, not a style.

    Imagine the size of the array if you had to put complex css styling in there.

    So you need to define your style in a css and only then you can apply a class to the bar:

    panelClass: ['first-class', 'second-class'];
    
    0 讨论(0)
  • 2021-01-04 19:38

    In my case all above doesn't work. It starts working when I add !important in css:

    .error-snackbar {
      background-color: fuchsia !important;
    }
    
    0 讨论(0)
  • 2021-01-04 19:46

    In angular 7 using ::ng-deep in front of class worked for me.

    ::ng-deep  .snackBar-fail {
        color: #ffffff !important;
        background-color: #cc3333 !important;
    }
    
    0 讨论(0)
  • 2021-01-04 19:50

    In your component SnackBarComponentExample try:

    saveButtonClick = () =>{
      let config = new MatSnackBarConfig();
      config.duration = 5000;
      config.panelClass = ['red-snackbar']
      this.snackBar.open("This is a message!", "ACTION", config);
    }
    

    Where 'red-snackbar' is a class in your apps main styles.css file. Strangely I was unable to get the config.panelClass to work when I was referencing my components associated css file, but once I put the class into the main styles.css file my styles were applied correctly to the snackBar.

    0 讨论(0)
提交回复
热议问题