How to add Icon inside the Angular material Snackbar in Angular 5

后端 未结 5 2017
情书的邮戳
情书的邮戳 2021-02-15 01:34

I am new to angular and I am using Angular Material Design for UI.

In my application I have a snackbar .

Now I want to set an Icon inside the snackbar but I trie

5条回答
  •  南笙
    南笙 (楼主)
    2021-02-15 02:06

    This how I did it

    1. Create a component
    2. Create a Service for the snackBar
    3. add the component to a module. import it in declarations and entryComponents
    4. Use the service

    example

    my-snackbar.component.ts

     import { Component, OnInit, Inject } from '@angular/core';
     import { MAT_SNACK_BAR_DATA } from '@angular/material/snack-bar';
    
        @Component({
          selector: 'my-snackbar',
          templateUrl: './snackbar.component.html',
          styleUrls: ['./snackbar.component.scss']
        })
        export class MySnackbarComponent implements OnInit {
          constructor(@Inject(MAT_SNACK_BAR_DATA) public data: any) {
            console.log(data); 
          }
    
          ngOnInit() {}
    
          get getIcon() {
            switch (this.data.snackType) {
              case 'Success':
                return 'done';
              case 'Error':
                return 'error';
              case 'Warn':
                return 'warning';
              case 'Info':
                return 'info';
            }
          }
        }
    

    .........

    my-snackbar.component.html

    {{getIcon}}
    {{data.message}}

    .........

    my-snack-bar.service.ts

    import { Injectable } from '@angular/core';
    import { MatSnackBar } from '@angular/material/snack-bar';
    import { MySnackbarComponent } from '../components/snackbar/my-snackbar.component';
    
    @Injectable({
      providedIn: 'root'
    })
    export class MySnackBarService {
      constructor(private snackBar: MatSnackBar) {}
      public openSnackBar(message: string, action: string, snackType?: snackType) {
        const _snackType: snackType =
          snackType !== undefined ? snackType : 'Success';
    
        this.snackBar.openFromComponent(SnackbarComponent, {
          duration: 2000,
          horizontalPosition: 'end',
          verticalPosition: 'top',
          data: { message: message, snackType: _snackType }
        });
      }
    }
    

    ........

    app.module.ts

    @NgModule({
      declarations: [
        SnackbarComponent
      ],
      imports: [
    ...
      ],
      providers: [],
      bootstrap: [AppComponent],
      entryComponents: [
        SnackbarComponent
      ]
    })
    export class AppModule {}
    

    .......

    other.component.ts

    import { Component, OnInit } from '@angular/core';
    import { MySnackBarService } from '../../services/my-snack-bar.service';
    
    @Component({
    ...
    })
    export class SomeComponent implements OnInit {
    
      constructor(
        private snack: MySnackService
      ) {}
    
      ngOnInit() {
      }
    
    
      openSnack() {
        this.snack.openSnackBar('Testing snack', '', 'Success');
      }
    }
    

提交回复
热议问题