我正在使用某些查询字符串参数在同一页面上调用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.
强制重新创建组件的另一种方法是使用自定义重用策略.另请参阅当使用不同的参数加载相同的url时,Angular2路由器2.0.0不会重新加载组件吗?(似乎没有太多可用的信息来实现它)
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
发布评论