简体   繁体   中英

Angular 2 router unable to navigate when called from OnInit of component

I am building a testportal where students can give test,so on OnInit of TestComponent .I get the complete test from a service and then route to the questions or the instruction component depending upon the state of test(paused or starting for the first time).

It is not working there,even though the promise returned by this.router.navigate is resolving,If I put my this.router.navigate code in a settimeout ,even with a timeout of 0 it works and navigates properly.

Any help on how to handle this case and details on why it is not navigating properly,I looked at the router's code but could not seem to find anything there. This is from angular2 final release.

TL;DR this.route .navigate is not working on the OnInit of component.

@angular/router: 3.0.0

@angular/common: 2.0.0

  RouterModule.forRoot(appRoutes, { initialNavigation: false });

My issue was because,I was trying to navigate to a different url,before inital navigation was over.Putting this line in app.routing.ts solved the issue.

https://github.com/angular/angular/issues/11748

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM