![](/img/trans.png)
[英]Angular CanDeactivate Guard Not Working Properly With MatDialog
[英]Angular CanDeactivate Guard not working with Sweet Alert JS
我在帶有Sweet Alert js 的Angular 中使用CanDeactivate
防護。 但是確定點擊不會被觸發。 我是 Angular 的新手,請幫忙。 這是帶有甜蜜警報的代碼。 顯示甜蜜警報,但確定單擊不起作用。
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;
}
}
但正常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;
}
}
我錯過了什么嗎?
您必須像正常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.