首页 文章

没有MatDialogRef的提供者

提问于
浏览
9

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

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

当我将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();
 }

}

app.module.ts

entryComponents: [DialogOverviewExample,DialogOverviewExampleDialog]

对话框的概述-示例-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>

5 回答

  • 11

    问题是您将 <dialog-component></dialog-component> 包含在代码中的某个位置,并使用@Inject将其注入到视图中 . 如果您从 <dialog-component></dialog-component> 中移除了 <dialog-component></dialog-component> ,那么它将起作用!

  • 4

    从DialogOverviewExample组件中删除MatDialogRef . 你不需要那里 . 它应该工作 .

  • 0

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

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

    并在模块声明中将 MatDialogModule 添加到 imports

    @NgModule({
      declarations: [...],
      imports: [
        ...
        MatDialogModule,
        ...
      ],
      providers: [...],
      entryComponents: [...],
      bootstrap: [...]
    })
    

    EDIT:

    您需要通过 this.dialogRef.close(); 传递数据以获取 subscribe 中的 result . 例如:

    @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);
     }
    
    }
    
  • 2

    你会得到那个确切的

    没有MatDialogRef的提供者

    如果在 dialog-overview-example.html 中包含对话框的组件html选择器,则会出现错误,如 <dialog-overview-example-dialog></dialog-overview-example-dialog>

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

  • 14

    我找到了一个简单的解决方案

    • 首先从Component构造函数中删除 MatDialogRef

    • 参考在 MatDialog 中打开的组件,如下所示

    let matDialogref = this.matDialog.open(CreateRawMaterialMasterComponent, matDialogConfig)

    • 使用上面的 matDialogRefafterClose 方法,如下所示 .
    openDialog(): void {
    
    let matDialogref = this.matDialog.open(CreateRawMaterialMasterComponent matDialogConfig)
    
    matDialogref.afterClosed().subscribe(
      (data) => {
        if (data) {
          this.loadGrid();
        }
      }
    );}
    
    • 不要使用 MatDialogRef 构造函数的公共引用 .

相关问题