简体   繁体   中英

this._portalOutlet is undefined when trying to open a MatSnackBar

I'm trying to open a MatSnackBar when I catch an error. I've simplified it to this but I still can't get it to work.

constructor(public matSnackBar: MatSnackBar) {
    this.matSnackBar.open('test', 'Undo', {
        duration: 3000
    });
}

I keep getting the same error: ERROR Error: "Uncaught (in promise): TypeError: this._portalOutlet is undefined .

I added MatSnackBarContainer to providers and entryComponents in my @NgModule

I solved it by importing MatSnackBarModule along with providing MatSnackBar and adding MatSnackBarContainer to entryComponents

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserAnimationsModule,
    MatSnackBarModule
  ],
  providers: [ MatSnackBar, {
    provide: MAT_DIALOG_DATA,
    useValue: {}
  }],
  bootstrap: [AppComponent],
  entryComponents: [MatSnackBarContainer]
})
export class AppModule {
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM