【发布时间】:2020-01-05 03:43:44
【问题描述】:
我成功构建了 canDeactivate 守卫,它在正常 confirm 下工作正常,但我想用角度材料对话框来实现它,在这里我遇到了一些问题。
这是我的守卫:
@Injectable()
export class CanDeactivateGuard implements CanDeactivate<CreateQuoteComponent> {
constructor(
public dialog: MatDialog,
){
}
canDeactivate(component: CreateQuoteComponent): boolean {
if (!component.changesSaved) {
return component.confirmDialog();
//return confirm('You have not saved your current work. Do you want to proceed and discard your changes?');
}
return true;
}
}
这是我组件中的一个函数:
confirmDialog(): boolean {
const message = 'You have not saved your current work. Do you want to proceed and discard?';
const data = { 'message': message, 'toShowCancel': true, 'buttonYesCaption': 'Yes', 'buttonNoCaption': 'No' };
const dialogRef = this.dialog.open(YesNoComponent, {
width: '600px',
height: '250px',
data: data
});
dialogRef.afterClosed().subscribe(result=>{
this.dialogRef1=result;
return this.dialogRef1;
});
return this.dialogRef1;
}
I defined a boolean variable dialogRef1 at the top of the component.
它是带有模态窗口的组件的一部分:
onCloseClick(){
this.dialogRef.close(false);
}
onSubmit(){
this.dialogRef.close(true);
}
但这对我不起作用,或者我做错了。提前致谢!
【问题讨论】: