canDeactivate 不适用于 Angular material 模态 window
canDeactivate doesn't work with Angular material modal window
我成功构建了 canDeactivate 防护装置,它在正常 confirm 下工作正常,但我想用 angular material 对话框实现它,这里我遇到了一些问题。
这是我的守卫:
@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.
它是具有模态的组件的一部分window:
onCloseClick(){
this.dialogRef.close(false);
}
onSubmit(){
this.dialogRef.close(true);
}
我尝试实现这个例子:
How to send return value to CanDeactivate Guard after close the mat-dialog | Angular CanDeactivate Guard | Angular Material Dialog
但它对我不起作用,或者我做错了。提前致谢!
您正在 returning 一个由 Observable [即 dialogRef.afterClosed()
] 设置的变量值,这将由用户决定。您应该执行以下操作:
首先,将 canDeactivate
的 return 类型更改为 Observable<boolean>
,如下所示:
@Injectable()
export class CanDeactivateGuard implements CanDeactivate<CreateQuoteComponent> {
constructor(
public dialog: MatDialog,
){
}
canDeactivate(component: CreateQuoteComponent): Observable<boolean> {
if (!component.changesSaved) {
return component.confirmDialog();
}
//please import 'of' form 'rxjs/operators'
return of(true);
}
}
现在让我们将 component.confirmDialog
方法更改为 return dialogRef.afterClosed()
observable,如下所示:
confirmDialog(): Observable<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
});
return dialogRef.afterClosed();
}
希望对您有所帮助。
我成功构建了 canDeactivate 防护装置,它在正常 confirm 下工作正常,但我想用 angular material 对话框实现它,这里我遇到了一些问题。
这是我的守卫:
@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.
它是具有模态的组件的一部分window:
onCloseClick(){
this.dialogRef.close(false);
}
onSubmit(){
this.dialogRef.close(true);
}
我尝试实现这个例子: How to send return value to CanDeactivate Guard after close the mat-dialog | Angular CanDeactivate Guard | Angular Material Dialog
但它对我不起作用,或者我做错了。提前致谢!
您正在 returning 一个由 Observable [即 dialogRef.afterClosed()
] 设置的变量值,这将由用户决定。您应该执行以下操作:
首先,将 canDeactivate
的 return 类型更改为 Observable<boolean>
,如下所示:
@Injectable()
export class CanDeactivateGuard implements CanDeactivate<CreateQuoteComponent> {
constructor(
public dialog: MatDialog,
){
}
canDeactivate(component: CreateQuoteComponent): Observable<boolean> {
if (!component.changesSaved) {
return component.confirmDialog();
}
//please import 'of' form 'rxjs/operators'
return of(true);
}
}
现在让我们将 component.confirmDialog
方法更改为 return dialogRef.afterClosed()
observable,如下所示:
confirmDialog(): Observable<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
});
return dialogRef.afterClosed();
}
希望对您有所帮助。