[英]Angular CanDeactivate Guard not working with Sweet Alert JS
I'm using CanDeactivate
guard in Angular with Sweet Alert js .我在带有Sweet Alert js 的Angular 中使用
CanDeactivate
防护。 But ok click is not fired.但是确定点击不会被触发。 I'm new to Angular Please assist.
我是 Angular 的新手,请帮忙。 Here is the code with sweet alert.
这是带有甜蜜警报的代码。 Sweet Alert is displayed but ok click not working.
显示甜蜜警报,但确定单击不起作用。
export class QuestionEditGuard implements CanDeactivate<FeedbackQuestionEditPage> {
canDeactivate(component: FeedbackQuestionEditPage): Observable<boolean> | Promise<boolean> | boolean {
if (component.questionForm.dirty) {
const question = component.questionForm.get('description').value || 'New Question';
swal.fire({
title: 'Hey there!!',
text: `Navigate away and lose all changes to ${question}?`,
type: 'warning',
showCancelButton: true,
confirmButtonText: 'OK',
}).then((result) => {
return true;
});
return false;
}
return true;
}
}
But with normal Confirm
it works.但正常
Confirm
它有效。
export class QuestionEditGuard implements CanDeactivate<FeedbackQuestionEditPage> {
canDeactivate(component: FeedbackQuestionEditPage): Observable<boolean> | Promise<boolean> | boolean {
if (component.questionForm.dirty) {
const question = component.questionForm.get('description').value || 'New Question';
return confirm(`Navigate away and lose all changes to ${question}?`);
}
return true;
}
}
Am I missing anything?我错过了什么吗?
You have to return
the Swal.fire
function as you do with the normal confirm
您必须像正常
confirm
一样return
Swal.fire
函数
export class QuestionEditGuard implements CanDeactivate<FeedbackQuestionEditPage> {
canDeactivate(component: FeedbackQuestionEditPage): Observable<boolean> | Promise<boolean> | boolean {
if (component.questionForm.dirty) {
const question = component.questionForm.get('description').value || 'New Question';
return swal.fire({ // <- return here
title: 'Hey there!!',
text: `Navigate away and lose all changes to ${question}?`,
type: 'warning',
showCancelButton: true,
confirmButtonText: 'OK',
}).then((result) => {
if (result.value) { // check if OK pressed
return true;
} else {
return false;
}
})
} else {
return true;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.