Angular 试图打开Matsnakbar时,此.\u portalOutlet未定义

Angular 试图打开Matsnakbar时,此.\u portalOutlet未定义,angular,angular-material,Angular,Angular Material,当我发现一个错误时,我正试图打开一个Matsnakbar。我已经把它简化为这个,但我仍然无法让它工作 constructor(public matSnackBar: MatSnackBar) { this.matSnackBar.open('test', 'Undo', { duration: 3000 }); } 我一直收到相同的错误:错误错误:“未捕获(承诺):TypeError:this.\u portalOutlet未定义 我在@NgModule中将Ma

当我发现一个错误时,我正试图打开一个Matsnakbar。我已经把它简化为这个,但我仍然无法让它工作

constructor(public matSnackBar: MatSnackBar) {
    this.matSnackBar.open('test', 'Undo', {
        duration: 3000
    });
}
我一直收到相同的错误:
错误错误:“未捕获(承诺):TypeError:this.\u portalOutlet未定义


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

我通过导入
MatSnackBarContainer
以及提供
MatSnackBarContainer
并将
MatSnackBarContainer
添加到entryComponents中解决了这个问题

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

我想知道你是否尝试在ngOninit中打开,你会得到同样的错误?它仍然是一样的,但页面显然已加载。你能提供完整的组件代码并尝试创建演示以在stackblitz中重现该问题吗?我在angular 8中面临同样的问题。尝试与angular.io教程中完全相同,但得到了这个错误。@VeeJay我解决了它或者我自己,看看我的答案。