没有MatDialogRef的提供者

kas*_*hen 16 angular-material2 angular

我正在使用材料2版本2.0.0-beta.12.我需要调整材质Modal的位置.我按照这个答案来做到这一点./sf/answers/3096708961/由于MdDialog现在不可用,我使用了{MatDialog,MatDialogRef,MAT_DIALOG_DATA}.

constructor(public dialog: MatDialog,public dialModRef:MatDialogRef<any>) {}
Run Code Online (Sandbox Code Playgroud)

当我将MatDialogRef添加到我的构造函数时,它给了我"没有MatDialogRef的提供程序"错误.请你帮我解决这个问题吗?

对话框的概述,example.ts

import {Component, Inject} from '@angular/core';
import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from 
'@angular/material';

@Component({
   selector: 'dialog-overview-example',
   templateUrl: 'dialog-overview-example.html'
})
export class DialogOverviewExample {

   animal: string;
   name: string;

   constructor(public dialog: MatDialog,public 
   dialModRef:MatDialogRef<any>) {}

   openDialog(): void {
       let dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
        width: '500px',
        data: { name: this.name, animal: this.animal }
   });

   dialogRef.afterClosed().subscribe(result => {
      console.log('The dialog was closed');
      this.animal = result;
   });
 }
}

@Component({
   selector: 'dialog-overview-example-dialog',
   templateUrl: 'dialog-overview-example-dialog.html',
})
export class DialogOverviewExampleDialog {

constructor(
  public dialogRef: MatDialogRef<DialogOverviewExampleDialog>,
@Inject(MAT_DIALOG_DATA) public data: any) { }

 onNoClick(): void {
   this.dialogRef.close();
 }

}
Run Code Online (Sandbox Code Playgroud)

app.module.ts

entryComponents: [DialogOverviewExample,DialogOverviewExampleDialog]
Run Code Online (Sandbox Code Playgroud)

对话框的概述-示例-dialog.html

<h1 md-dialog-title>Hi {{data.name}}</h1>
<div md-dialog-content>
  <p>What's your favorite animal?</p>
  <md-form-field>
    <input mdInput tabindex="1" [(ngModel)]="data.animal">
  </md-form-field>
</div>
<div md-dialog-actions>
  <button md-button tabindex="2">Ok</button>
  <button md-button (click)="onNoClick()" tabindex="-1">No Thanks</button>
</div>
Run Code Online (Sandbox Code Playgroud)

Jav*_*les 21

你会得到那个确切的

没有提供商 MatDialogRef

如果错误您在你的对话框的组件html选择dialog-overview-example.html像<dialog-overview-example-dialog></dialog-overview-example-dialog>

没有必要,不要在任何地方包含对话框组件的html选择器.

  • 谢谢哈维尔,这简直要了我的命,你的回答解决了这个问题 (2认同)
  • 谢谢哈维尔,我真的很讨厌有角度的方面。就像这些错误是故意模糊的 (2认同)

Pio*_*zko 18

您需要在模块文件中导入此模块(app.module.ts默认情况下):

import { MatDialogModule } from '@angular/material';
Run Code Online (Sandbox Code Playgroud)

并添加MatDialogModule到imports您的模块声明:

@NgModule({
  declarations: [...],
  imports: [
    ...
    MatDialogModule,
    ...
  ],
  providers: [...],
  entryComponents: [...],
  bootstrap: [...]
})
Run Code Online (Sandbox Code Playgroud)

编辑:

你需要传递的数据通过this.dialogRef.close();得到result的subscribe工作.例如:

@Component({
   selector: 'dialog-overview-example-dialog',
   templateUrl: 'dialog-overview-example-dialog.html',
})
export class DialogOverviewExampleDialog {
someValue: boolean = false;

constructor(
  public dialogRef: MatDialogRef<DialogOverviewExampleDialog>,
@Inject(MAT_DIALOG_DATA) public data: any) { }

 onNoClick(): void {
   this.dialogRef.close(this.someValue);
 }

}
Run Code Online (Sandbox Code Playgroud)


Chr*_*ert 10

2021 年更新

No provider for MatDialogRef从导入时导致错误的 Angular 错误@angular/material/dialog已修复。

正确的导入方式MatDialogRef是再次使用特定的组件入口点:

import {MatDialog} from '@angular/material/dialog';
Run Code Online (Sandbox Code Playgroud)

角材料模块

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

@NgModule({
  exports: [
    MatDialogModule,
    ...
  ]
})
export class MyMaterialModule {}
Run Code Online (Sandbox Code Playgroud)

 

原始答案 2019 年 9 月

许多Angular Material 示例将MatDialogRef导入显示为:

import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from '@angular/material/dialog';
Run Code Online (Sandbox Code Playgroud) 这将导致以下**错误**(例如,在创建 Service 时包装对话框组件的创建):
ERROR NullInjectorError: StaticInjectorError(AppModule)[DialogComponent -> MatDialogRef]: 
  StaticInjectorError(Platform: core)[DialogWarningComponent -> MatDialogRef]: 
    NullInjectorError: No provider for MatDialogRef!
Run Code Online (Sandbox Code Playgroud)

正确导入:'@angular/material'

从“@angular/material”导入{MatDialog、MatDialogRef、MAT_DIALOG_DATA};

  • 组件不再可以通过“@angular/material”导入。使用单独的辅助入口点,例如@angular/material/button。 (3认同)

小智 5

问题是您要<dialog-component></dialog-component>在代码中包含某个位置,并使用@Inject将其注入视图中。如果您将其中的<dialog-component></dialog-component>template.html 删除,它将可以正常工作!


小智 5

您注入MatDialogRef了DialogOverviewExample打开对话框的组件的构造函数,但它不是对话框。

MatDialogRef只能在作为对话框打开的组件中注入,否则dialogRef会触发此错误。