ionic-framework icon indicating copy to clipboard operation
ionic-framework copied to clipboard

NavController rootNav Property

Open T-Specht opened this issue 10 years ago • 4 comments

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?

T-Specht avatar Aug 11 '16 06:08 T-Specht

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.

T-Specht avatar Aug 11 '16 06:08 T-Specht

Well, as it seems the solution above does only works in Browser. On the mobile Device it doesn't work.

T-Specht avatar Aug 11 '16 07:08 T-Specht

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 avatar Aug 11 '16 09:08 andersfa87

@andersfa87 Thanks, this works!

T-Specht avatar Aug 11 '16 09:08 T-Specht