问题描述:
在使用Angular 9 Material Dialog时,关闭Dialog后,返回的结果为undefined。
解决方法:
import { Component, OnInit } from '@angular/core';
import { MatDialogRef } from '@angular/material/dialog';
@Component({
selector: 'app-dialog',
templateUrl: './dialog.component.html',
styleUrls: ['./dialog.component.css']
})
export class DialogComponent implements OnInit {
constructor(public dialogRef: MatDialogRef) { }
ngOnInit(): void {
}
closeDialog(result: any): void {
this.dialogRef.close(result);
}
}
import { Component, OnInit } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { DialogComponent } from './dialog.component';
@Component({
selector: 'app-parent',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.css']
})
export class ParentComponent implements OnInit {
constructor(public dialog: MatDialog) { }
ngOnInit(): void {
}
openDialog(): void {
const dialogRef = this.dialog.open(DialogComponent, {
width: '250px',
data: { name: 'John' }
});
dialogRef.afterClosed().subscribe(result => {
console.log('Dialog result:', result);
});
}
}
这样,在关闭Dialog时,会将结果传递给订阅afterClosed()的组件,并打印出结果。
希望以上解决方法对你有帮助!