Angular 2 Material MdSnackBar no Provider

Viewed 12073

I want a simple Snackbar popping up on the click of a button. But I always get the error:

ERROR Error: No provider for MdSnackBar! at injectionError (core.es5.js:1169) at noProviderError (core.es5.js:1207

import {MdSnackBar} from '@angular/material';
constructor(public snackBar: MdSnackBar){
        this.snackBar.open('hello')
    }
2 Answers

If you have version "@angular/material": "2.0.0-beta.12" or higher, you should import MatSnackBarModule

import {MatSnackBarModule} from '@angular/material';

// Use a more specific import if you're using "@angular/material": "8.0.0" and higher
// import {MatSnackBarModule} from '@angular/material/snack-bar';

...

@NgModule({
      imports: [
            ...
            MatSnackBarModule
          ],
    ...
Related