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

M_F*_*and 6 angular-material2 angular

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

Edr*_*ric 6

您可以使用componentInstanceMatDialogRef<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;
}
Run Code Online (Sandbox Code Playgroud)

对话框组件:

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