路由器导航在同一页面时不调用ngOnInit [英] Router Navigate does not call ngOnInit when same page

查看:147
本文介绍了路由器导航在同一页面时不调用ngOnInit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用某些查询字符串参数在同一页面上调用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆