在调用MatDialog的时候添加afterClosed()方法,并在该方法中进行需要在 MatDialog 中完成的操作,以确保它们在对话框关闭后执行。
示例代码如下:
import { MatDialog } from '@angular/material/dialog';
export class MyComponent implements OnDestroy {
dialogRef: MatDialogRef
constructor(private dialog: MatDialog) {}
openDialog(): void { this.dialogRef = this.dialog.open(MyDialogComponent);
this.dialogRef.afterClosed().subscribe(result => {
// 在此处添加需要在 MatDialog 中完成的操作
});
}
ngOnDestroy() { if (this.dialogRef) { this.dialogRef.close(); } } }