I had this ionViewCanLeave method to can leave a page. Now, in ionic 4, I have to use a guard with canDeactivate. But I need to convert this code to return a promise but only when a button (ok or cancel) is clicked.
canDeactivate(): boolean | Promise<boolean> | Observable<boolean> {
// If the support message is empty we should just navigate
if (!this.f.controls['consulta'].value || this.f.controls['consulta'].value.trim().length === 0) {
return true;
}
// return this.confirmDialogService.confirm('Quieres cancelar el mensaje? Tu mensaje no será enviado!');
return new Promise((resolve: any, reject: any) => {
this.alertCtrl.create({
header: 'Abandonar?',
message: 'Quieres cancelar el mensaje? Tu mensaje no será enviado!',
buttons: [
{ text: 'Permanecer', handler: reject },
{ text: 'Abandonar', role: 'cancel', handler: resolve }
]
}).then(alert => alert.present());
});
}
I have tried some options, but nothing works. But I use a window.confirm instead an alertCtrl, works as expected, like this:
confirm(message?: string): Observable<boolean> {
const confirmation = window.confirm(message || 'Are you sure?');
return Observable.of(confirmation);
}
Someone can help me?