要在Angular 8中使用MatDialog等待结果并继续执行代码,可以使用Promise和async/await方法。以下是一个示例解决方案:
import { MatDialog } from '@angular/material/dialog';
export class YourComponent {
constructor(private dialog: MatDialog) { }
openDialog(): Promise {
return new Promise((resolve, reject) => {
const dialogRef = this.dialog.open(YourDialogComponent);
dialogRef.afterClosed().subscribe(result => {
if (result) {
resolve(result);
} else {
reject();
}
});
});
}
}
async yourMethod() {
try {
const result = await this.openDialog();
// 在这里使用结果进行进一步的操作
} catch (error) {
// 处理错误
}
}
import { MatDialogRef } from '@angular/material/dialog';
export class YourDialogComponent {
constructor(private dialogRef: MatDialogRef) { }
onConfirm() {
// 执行一些操作,然后关闭对话框并传递结果
this.dialogRef.close('确认');
}
onCancel() {
// 执行一些操作,然后关闭对话框并传递结果
this.dialogRef.close('取消');
}
}
通过使用Promise和async/await方法,你可以在等待MatDialog结果时暂停执行代码,并在结果可用时继续执行。