Angular2如何重建视图(路由)

Angular2 how to rebuild a view (routing)

如何重建路由器?当我加载同一个路由器但使用另一个参数时,只有 url 发生变化,视图保持不变。有什么方法可以说路由器 - "Hey, rebuild yourself!"?

我需要类似 window.location.reload() 的效果,但无需重新加载。

当您使用不同的参数导航到同一路线时,它会重用该组件。因此 ngOnInit 不会再被调用,因此您可能认为页面没有更新。您应该在 ngOnInit 中订阅 routeparam,然后在订阅的函数中进行视图更新

在构造函数中注入 Activated 路由

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);
   });
}

再看看另一个这样