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

Ionic2 - nav.setRoot(Component) - Works only first time, unable to navigate after, no errors reported

Open tonyawad88 opened this issue 10 years ago • 100 comments

Short description of the problem:

I am developing an app with Ionic2. I have App.ts as the parent, Welcome.ts as a child page and Featured.ts as a child page as well. I have firebase integrated with the app. Rootpage is first set to Welcome.

this.rootPage = Welcome;

Step 1: I click a button on Welcome to authenicate with Google, once onAuth is called, I set this.nav.setRoot(Featured); All works fine up to now. I get to the Featured page which enables a sidemenu with different pages and i am able to navigate to all of them no problem.

Step 2: Featured enables a sidemenu from App.ts/html, in there I have a logout button that calls unauth. Unauth does some Firebase unauth and then:

this.menu.close();    
let nav = this.app.getComponent('nav');    
nav.setRoot(Welcome); 

Step 3: Login again same as in step 1. All is fine up to now.

Step 4: Open side menu, selects any of the pages from the sidemenu, navigation doesn't occur. sidemenu closes but page remain the same, navbar icon button disappear and no navigation occurs. openPage code:

  openPage(page) {
    //1. Loop through and set active attribute to False if it doesn't match.
    //   Set to True if it matches page title
    for(var i = 0; i < this.NavPages.length; i++) {
      if(this.NavPages[i].title == page.title) this.NavPages[i].active = true;
      else this.NavPages[i].active = false;
    }
    for(var i = 0; i < this.ActivityPages.length; i++) {
      if(this.ActivityPages[i].title == page.title) this.ActivityPages[i].active = true;
      else this.ActivityPages[i].active = false;
    }
    for(var i = 0; i < this.PreferencesPages.length; i++) {
      if(this.PreferencesPages[i].title == page.title) this.PreferencesPages[i].active = true;
      else this.PreferencesPages[i].active = false;
    }

    //2. close the menu when clicking a link from the menu
    this.menu.close();
    //3. navigate to the new page if it is not the current page
    let nav = this.app.getComponent('nav');
    nav.setRoot(page.component);
  }

What behavior are you expecting?

I am expecting navigation to continue to occur, similar to what happens in step 1.

Steps to reproduce: Steps to reproduce included above.

Code included above.

Other information: (e.g. stacktraces, related issues, suggestions how to fix, stackoverflow links, forum links, etc) No error is being thrown unfortunately. I don't see any erros in the console logs.

Which Ionic Version? 2.x

Run ionic info from terminal/cmd prompt: (paste output below)

Your system information:

Cordova CLI: 6.0.0
Gulp version:  CLI version 3.9.0
Gulp local:   Local version 3.9.1
Ionic Version: 2.0.0-beta.3
Ionic CLI Version: 2.0.0-beta.19
Ionic App Lib Version: 2.0.0-beta.9
ios-deploy version: 1.8.2 
ios-sim version: 5.0.3 
OS: Mac OS X El Capitan
Node Version: v0.12.7
Xcode version: Xcode 7.2.1 Build version 7C1002 

Thank you.

tonyawad88 avatar Mar 18 '16 19:03 tonyawad88

I noticed the following: 2 ion pages are present at once in the dom. screen shot 2016-03-18 at 3 34 03 pm

The one I navigated to is "explore" which has a lower z-index(92) than the "featured" page. Therefore I should be on explore but the "featured" is overlayed on top of "explore" with a z-index of 93.

tonyawad88 avatar Mar 18 '16 19:03 tonyawad88

I'm experiencing the same issue but in my case I can't even get redirected after login.

If I'm already logged and land in the login view, nav.setRoot(TabsPage) is executed perfectly.

But if I log-in with facebook after getting the user probile nav.setRoot(TabsPage) is doing nothing. Moreover, nav.push(TabsPage) is also doing nothing, but if I do nav.push(Main) where Main is one of my tabs, is executing correctly, but I don't have tabbar and a back button appears, obviously.

Also if I do nav.setRoot(Main) it works correctly, but I land in a tab without tabbar and with back button.

jorchg avatar Mar 29 '16 11:03 jorchg

I add some information. I saw in the docs that setRoot return a promise, so I did this:

this.nav.setRoot(TabsPage)
  .then(data => {
      console.log(data);
  }, (error) => {
      console.log(error);
  })

This throws true when I land on the login page and I'm already logged, but throws nothing when I login with facebook, so it seems that the promise is never resolved.

jorchg avatar Mar 29 '16 11:03 jorchg

Ok, another update. If you go to the core of Ionic2 to components/nav/nav-controller.js in the function setPages which is called from setRoot the callback of the transition is never been executed. If you follow the flow of functions called: setRoot -> setPages -> _transition -> _render -> _postRender

In the _render function:

this.loadPage(enteringView, null, opts, function () {
    if (enteringView.onReady) {
        // this entering view needs to wait for it to be ready
        // this is used by Tabs to wait for the first page of
        // the first selected tab to be loaded
        enteringView.onReady(function () {
            enteringView.loaded();
            _this._postRender(transId, enteringView, leavingView, isAlreadyTransitioning, opts, done);
        });
    }
    else {
        enteringView.loaded();
        _this._postRender(transId, enteringView, leavingView, isAlreadyTransitioning, opts, done);
    }
});

My code enters in enteringView.onReady and the callback is never executed.

If I just call the _postRender method before waiting for enteringView.onReady my app works fine, but It's so dirty. The problem seems to be waiting for the page of the tab to be ready. I think it never gets ready or never acquire fine the ready state.

Could be this something with my code? Or is likely to be a bug?

Thank you!

jorchg avatar Mar 29 '16 12:03 jorchg

I have the same problem with Google or Facebook auth

privetr avatar Apr 05 '16 07:04 privetr

@privetr this seems to have been fixed with beta4. Can you please check ? Mine is working fine now.

tonyawad88 avatar Apr 08 '16 03:04 tonyawad88

@jorchg Can you check as well ? when you get a chance :)

tonyawad88 avatar Apr 08 '16 03:04 tonyawad88

Hello all! As it looks like this was fixed in beta.4 i will be closing this issue, but if you are still having this issue feel free to comment!

jgw96 avatar Apr 08 '16 04:04 jgw96

I am still having this issue. I can't get the nav to push or setRoot to my TabsPage after a Facebook login promise.

Cordova CLI: 6.1.1
Gulp version:  CLI version 1.2.1
Gulp local:   Local version 3.9.1
Ionic Framework Version: 2.0.0-beta.4
Ionic CLI Version: 2.0.0-beta.24
Ionic App Lib Version: 2.0.0-beta.14
ios-deploy version: 1.8.5
ios-sim version: 5.0.8
OS: Mac OS X El Capitan
Node Version: v5.9.1
Xcode version: Xcode 7.3 Build version 7D175

tlaverdure avatar Apr 08 '16 21:04 tlaverdure

I also am experiencing the situation where if I call nav.setRoot(x), the new page doesn't actually appear. Yet, if I focus an input field in the current page, it disappears and shows the correct page.

Additionally, if I push onto the stack instead of setRoot, the animation doesn't happen until I focus an input field.

Same problem.

Edit: Just swapped out the navigation in my app to a sidemenu and everything is working fine. So the tabs are borked as of beta 4 FYI.

daveshirman avatar Apr 18 '16 15:04 daveshirman

@daveshirman are you using tabs to navigate around ? It might be related to tabs, my issue was related to sidemenu. However they both use the navcontroller, not sure why it would work for me and not for you.. Maybe produce a plunker or open a new issue and the ionic team will be able to help out.

tonyawad88 avatar Apr 18 '16 16:04 tonyawad88

I was having the same issue : nav.setRoot(MyComponent) did not seem to do anything. I was running the app in Android using --live-reload, and there were warnings about some events not being fired :

0     195445   log      deviceready has not fired after 5 seconds.
1     195457   log      Channel not fired: onDOMContentLoaded

I also could not test the code in the browser because it's about displaying a certain page when the device goes offline.

So I finally tried to run my code in Android, but without the live reload, and it works fine.

So, there is something about the live reload mode that is preventing it from working. Using Beta4 as my dependency, and the CLI version 2.0.0-beta25.

HereThereBeMonsters avatar Apr 20 '16 13:04 HereThereBeMonsters

I'm still experiencing the issue I described. I completely forgot, and after a ionic update it stopped working, so I just modified it again as described in my latest post in this thread.

Should I open another issue?

jorchg avatar Jun 15 '16 12:06 jorchg

Hi Everyone. Did anyone find a fix for this? I am currently using Ionic [email protected] and the problem is still present!

marianocodes avatar Jun 18 '16 02:06 marianocodes

I found the cause for my issue was calling .pop() when only setRoot had been called.

TheMadBug avatar Jul 05 '16 02:07 TheMadBug

I found that calling it from my app.ts seems to work reliably. Calling it from any other page seems to fail. Additionally, if I am not on a root page, like 3 pages deep, I also call nav.popToRoot() after the setRoot method, which works:

this.nav.setRoot(xxxx).then(() =>{
    this.nav.popToRoot();
    //....
});

daveshirman avatar Jul 05 '16 09:07 daveshirman

Why is this issue closed? Navigation seems really broken. If i push a page with forms in it it wont work either.

foliodev avatar Jul 13 '16 02:07 foliodev

Also breaks when there's a modal window showing too. On 13 Jul 2016 3:59 a.m., "scorpnode" [email protected] wrote:

Why is this issue closed? Navigation seems really broken. If i push a page with forms in it it wont work either.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/driftyco/ionic/issues/5883#issuecomment-232241198, or mute the thread https://github.com/notifications/unsubscribe/ANnlXmqTT5pf0i-JZNog5F32j8pB785Qks5qVFSZgaJpZM4H0Jz0 .

daveshirman avatar Jul 13 '16 07:07 daveshirman

Hi, this is still occurring to me in 2.0.0-beta.10. I'm getting a blank screen after calling NavController.setRoot. Only occurring on iOS devices

device info:

  • model: iPad5,4
  • iOS version: 8.3

nicopeeters avatar Jul 13 '16 15:07 nicopeeters