代码之家  ›  专栏  ›  技术社区  ›  M_Farahmand

如何从被调用组件内部访问MatDialogConfig?

  •  4
  • M_Farahmand  · 技术社区  · 7 年前

    我在spa中使用MatDialog,需要将MatDialogConfig传递给已调用的组件。有什么办法吗?

    1 回复  |  直到 4 年前
        1
  •  4
  •   Edric Prince Bansal    7 年前

    您可以使用 componentInstance 属于 MatDialogRef<T> 。只需获取对话框的引用即可获取 MatDialogRef :

    打开对话框的方法:

    openDialog(dialogConfig: MatDialogConfig) {
      let dialogRef = this.dialog.open(MyDialogComponent);
      // You can rename the dialogConfig instance to whatever you want. See the next code snippet for more info.
      dialogRef.componentInstance.dialogConfig = dialogConfig;
    }
    

    对话框组件:

    export class MyDialogComponent implements OnInit {
      // Rename the property to whatever you want it to be
      dialogConfig: MatDialogConfig;
      ngOnInit() {
        console.dir(`Dialog config: ${this.dialogConfig}`);
      }
    }