Angular2强制组件在路由时重新加载,即使url未更改

Angular2强制组件在路由时重新加载,即使url未更改,angular,Angular,在我的页面上,有一个按钮,单击该按钮可以根据用户的输入更改带有参数的路由 注意:此表单始终位于页面顶部 HTML: 应用程序路由: ... path:'chen', children:[ {path:'house/:id', component:HouseComponent} ... ] 在HouseComponent>ngOnInit方法中,使用路由发送的参数调用某个服务 现在问题是: 如果用户插入id并单击按钮,则URL更改为ok,并且HouseComponent加载良好 但若

在我的页面上,有一个按钮,单击该按钮可以根据用户的输入更改带有参数的路由

注意:此表单始终位于页面顶部

HTML:

应用程序路由:

...
path:'chen',
children:[
   {path:'house/:id', component:HouseComponent}
   ...
]
在HouseComponent>ngOnInit方法中,使用路由发送的参数调用某个服务

现在问题是:

如果用户插入id并单击按钮,则URL更改为ok,并且HouseComponent加载良好

但若用户现在用相同的id再次单击按钮,那个么URL不会改变,这很好,但正因为如此,组件不会再次加载,所以ngOnInit不会再次被调用

对于我们来说,这不是预期的行为,因为在HouseComponent的ngOnInit中调用的服务可能会在一段时间后返回不同的详细信息,因此它应该在每次单击按钮时再次调用该服务

即使URL没有更改,我如何强制组件重新加载

尝试window.reload


您可以按如下方式导航后调用您的服务:

this.router.navigate(['chen/house',form.id])
  .then(() => this.callYourService());


那么呢?@downvoter我想知道真正有助于我改善未来工作的原因。这两个问题对我来说都是问题:1。服务结果正在HouseComponentHTML中使用,因此从这个表单调用它是不好的。2.被调用的ngOnInit是表单组件中的一个,而不是HouseComponent中的一个
...
path:'chen',
children:[
   {path:'house/:id', component:HouseComponent}
   ...
]
openHouseDetails(){
   const form = this.houseFrom.value;
   if(!this.router.url.includes(`chen/house/${form.id}`))//check if user is on current page
   this.router.navigate(['chen/house',form.id]);
   else
   location.reload()
}
this.router.navigate(['chen/house',form.id])
  .then(() => this.callYourService());
this.router.navigate(['chen/house',form.id])
  .then(() => this.ngOnInit());