尝试打开MatSnackBar

时间:2019-11-26 15:21:47

标签: angular angular-material

当我发现错误时,我正在尝试打开MatSnackBar。我已经将其简化为这个,但仍然无法正常工作。

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

我一直收到相同的错误:ERROR Error: "Uncaught (in promise): TypeError: this._portalOutlet is undefined

我在我的@NgModule中将MatSnackBarContainer添加到了提供者和entryComponents

1 个答案:

答案 0 :(得分:0)

我通过导入MatSnackBarModule并提供MatSnackBar并将MatSnackBarContainer添加到entryComponents来解决了这个问题

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