Reputation: 2262
According to Ionic 3 documentation, this is supposed to work:
ionViewWillLeave(): boolean {
if (this.cantLeave) {
let alert = this.alertCtrl.create({
title: 'You can\'t leave',
subTitle: 'You stay and work.',
buttons: ['Oh sorry']
});
alert.present();
}
return this.cantLeave;
}
Yet it doesn't. The alert appears, but the page changes nonetheless. However here it says that return true
or return false
will tell Ionic to carry on with the page change or stop it.
http://ionicframework.com/docs/api/navigation/NavController/
What am I doing wrong?
Upvotes: 2
Views: 3071
Reputation: 29614
You need to use Nav Guards. Check the Nav Guards section here.
The lifecycle hook is ionViewCanLeave()
not ionViewWillLeave
ionViewCanLeave(): boolean {
if (this.cantLeave) {
let alert = this.alertCtrl.create({
title: 'You can\'t leave',
subTitle: 'You stay and work.',
buttons: ['Oh sorry']
});
alert.present();
}
return this.cantLeave;
}
Upvotes: 6