NavController rootNav Property
I am building a Tabs Application with a Login Page in Ionic 2. Therefore I have two NavControllers: one in the app.ts file and the other one for the Views of the tabs.
Now I want to navigate from a Tab View to my Login page.
this.nav.setRoot(LoginPage); does not work for me, because then the tabs are still displayed under the login page.
So I have to access the parent navController. I found this Link where my problem is solved like this:
this.nav.rootNav.setRoot(LoginPage);
However, TypeScript gives me an error, saying that the property does not exist (rootNav). But if I log out this.nav with console.log(this.nav), it certainly is there.
What should I do to solve this problem? Are there other solutions for accessing the parent navController?
Ok, so I just got it working like this:
this.nav.parent.viewCtrl._nav.setRoot(LoginPage);
Is this the correct way of doing it? It felt kind of hacky because there was no autocompletion for the parent.
Well, as it seems the solution above does only works in Browser. On the mobile Device it doesn't work.
You can use this function to get the root NavController.
export function getRootNav(nav: NavController) : NavController {
let root = nav;
while(root.parent != null){
root = root.parent;
}
return root;
}
and use it like:
let rootNav = getRootNav(this.nav);
rootNav.setRoot(LoginPage);
@andersfa87 Thanks, this works!