Angular NullInjectorError:没有 MatDialog 提供程序

Bra*_*ler 11 angular-material angular

因此,当我尝试加载我的 angular 应用程序时,出现此错误:

ERROR Error: Uncaught (in promise): 
NullInjectorError: StaticInjectorError(AppModule)[AppComponent -> MatDialog]:    
StaticInjectorError(Platform: core)[AppComponent -> MatDialog]:
Run Code Online (Sandbox Code Playgroud)

我的 ts 文件看起来像这样,每个其他帮助问题都说要将 MatDialog 添加到我的 NgModule 导入中,但我已经这样做了并且仍然收到错误。大多数这些错误是 StaticInjectorErrors 而我的是 NullInjectorError 但我不确定这两者之间有什么区别。

ERROR Error: Uncaught (in promise): 
NullInjectorError: StaticInjectorError(AppModule)[AppComponent -> MatDialog]:    
StaticInjectorError(Platform: core)[AppComponent -> MatDialog]:
Run Code Online (Sandbox Code Playgroud)

Bra*_*son 16

在我的情况下,我在尝试构建对话框组件时看到此错误,例如:

my-dialog.component.ts

<mat-dialog-container>
  <mat-dialog-content>
    <p>Hello</p>
    <p>I am a dialog</p>
  </mat-dialog-content>
  <mat-dialog-actions>
    <button mat-raised-button>Click me</button>
  </mat-dialog-actions>
</mat-dialog-container>
Run Code Online (Sandbox Code Playgroud)

问题是我正在使用mat-dialog-container. 这对于您将通过渲染的组件不是必需的matDialog.open(MyDialogComponent)

相反,只需mat-dialog-container完全删除并仅使用子节点。


Fah*_*san 9

请注意,Angular Material您必须将所有元素module导入到您的module.ts. 你必须module.ts像这样导入和添加模块

import {MatDialogModule} from '@angular/material/dialog';


import : [MatDialogModule]
Run Code Online (Sandbox Code Playgroud)

有关参考,请参阅此处的官方文档中的示例