Angular rc3 router - Navigating to same page with different parameters

China☆狼群 提交于 2019-12-05 02:25:25

When you navigate to same route with different param it reuse the component. Hence ngOnInit won't be called again. You should subscribe to routeparam in ngOnInit and then do the view update in subscribed function

Inject Activated route in constructor

constructor(
  private route: ActivatedRoute,
  private router: Router,......) {}

In the ngOnInit method, we use the ActivatedRoute service to retrieve the parameters for our route

ngOnInit() {
  this.sub = this.route.params.subscribe(params => {
     let id = +params['id']; // (+) converts string 'id' to a number
     //here goes your logic like below
this.service.getHero(id).then(hero => this.hero = hero);
   });
}

For more details see and section "Getting the route parameter "

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!