简体   繁体   English

导航到路由参数时未调用ngOnInit

[英]ngOnInit not called when navigating to route parameter

I've been having this issue for a while now with every version of Angular I've used, but have fudged my way around it (currently on 6.0.3). 我使用的每个Angular版本现在都遇到这个问题已有一段时间了,但是我已经绕过了这个问题(目前在6.0.3上)。 I've got routes setup like this: 我有这样的路由设置:

/book/:param

And I'm reading the parameter in ngOnInit fine, I can display it in the template etc. After that I have a quick way of setting a pretty version of the parameter which I actually want to display but this is never updated after the first time either route is navigated to. 而且我正在ngOnInit中很好地读取参数,可以在模板等中显示它。之后,我可以快速设置实际上要显示的参数的漂亮版本,但是在第一次之后就永远不会更新任一路线都导航到。 The same goes for anything I do differently for each route. 对于每条路线我所做的不同操作也是如此。

Should I just make separate components instead of using parameters or am I missing something obvious? 我应该只制造单独的组件而不使用参数,还是缺少明显的东西?

No need to write routing configuration like this - 无需编写这样的路由配置-

/book/:all
/book/:notconfirmed

Just replace this with a single line like this - 只需将其替换为这样的单行-

/book/:flag

Now what you have to do is, Just subscribe to params coming from routing and do whatever you want to do. 现在,您需要做的就是,只需订阅来自路由的params并执行您想做的任何事情。

this.activatedRoute.params.subscribe(params => {
   console.log('every time route params chnages you will get here');
}

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

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