【发布时间】:2016-11-17 09:09:42
【问题描述】:
如何重建路由器?当我加载同一个路由器但使用另一个参数时,只有 url 发生变化,视图保持不变。有没有办法说路由器 - “嘿,重建你自己!”?
我需要类似window.location.reload() 的效果,但无需重新加载。
【问题讨论】:
如何重建路由器?当我加载同一个路由器但使用另一个参数时,只有 url 发生变化,视图保持不变。有没有办法说路由器 - “嘿,重建你自己!”?
我需要类似window.location.reload() 的效果,但无需重新加载。
【问题讨论】:
当您使用不同的参数导航到相同的路线时,它会重用该组件。因此ngOnInit 不会被再次调用,所以您可能认为页面没有更新。您应该在 ngOnInit 中订阅 routeparam,然后在订阅函数中进行视图更新
在构造函数中注入激活的路由
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);
});
}
另外看看另一个soquestion
【讨论】: