Can't see the point at present to use such a navigationguard because we don't know about the target page to navigate to (active page info is avaible from the navController getActive() method). Typically we'll be using an alert to ask the question "Do you really want to leave this page?" in response to a menu option being selected for example and on OK, as far as I can tell, we don't know which page to "setRoot" to as the target page because the ionViewCanLeave handler has no parameters such as toPage/enteringPage.
Has anyone managed to do this yet or rather find out from the navigation objects in ionic2 target page information?
Here a little code to set context:
ionViewCanLeave(): boolean { // no parameters on offer :-(
let result: boolean = false;
let alert: Alert = this._alertCtrl.create({
title: 'Are you sure you want to leave..?',
message: 'Your changes will be lost if you do?',
buttons: [
{
text: 'Cancel',
role: 'cancel',
handler: () => {
let dismiss: boolean = true;
return dismiss;
}
},
{
text: 'OK',
handler: () => {
let dismiss: boolean = false;
let alertPop = alert.dismiss();
alertPop
.then(() => {
let rootNav: NavController = this._appCtrl.getRootNav();
this._navCtrl.pop();
this._navCtrl.setRoot(DashboardPage); // Here we want to parameterize to target page - hardcoded for now
});
return dismiss;
}
}
]
});
if (this._applicationService.getOfferStatus() === OfferStatus.live) {
alert.present();
}
return result;
}