简体   繁体   English

从组件的 OnInit 调用时,Angular 2 路由器无法导航

[英]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).我正在构建一个测试门户,学生可以在其中进行测试,因此在TestComponent OnInit上。我从服务中获得完整的测试,然后根据测试状态(暂停或第一次开始)路由到问题或指令组件.

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.它在那里不起作用,即使this.router.navigate返回的承诺正在解决,如果我将this.router.navigate代码放在settimeout ,即使超时为 0 它也能正常工作和导航。

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.这是来自angular2的最终版本。

TL;DR this.route .navigate is not working on the OnInit of component. TL;DR this.route .navigate不适用于组件的OnInit

@angular/router: 3.0.0 @角度/路由器:3.0.0

@angular/common: 2.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.我的问题是因为,在初始导航结束之前,我试图导航到不同的 url。将这一行放在 app.routing.ts 中解决了这个问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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