路由器导航在同一页面时不调用ngOnInit [英] Router Navigate does not call ngOnInit when same page
问题描述
我正在使用某些查询字符串参数在同一页面上调用router.navigate
.在这种情况下,ngOnInit()
不会调用.是默认设置还是我需要添加其他内容?
I am calling router.navigate
on same page with some query string parameters. In this case, ngOnInit()
does not call. Is it by default or do I need to add anything else ?
推荐答案
您可以注入ActivatedRoute
并订阅params
constructor(route:ActivatedRoute) {
route.params.subscribe(val => {
// put the code from `ngOnInit` here
});
}
路由器仅在导航到其他路线时销毁并重新创建组件.当仅更新路由参数或查询参数但路由相同时,该组件将不会被销毁和重新创建.
The router only destroys and recreates the component when it navigates to a different route. When only route params or query params are updated but the route is the same, the component won't be destroyed and recreated.
An alternative way to force the component to be recreated is to use a custom reuse strategy. See also Angular2 router 2.0.0 not reloading components when same url loaded with different parameters? (there doesn't seem to be much information available yet how to implement it)
这篇关于路由器导航在同一页面时不调用ngOnInit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!