2016-07-14 53 views
0

如何重建路由器?当我加载相同的路由器,但与另一个参数,只有URL更改,视图保持不变。有没有办法说路由器 - “嘿,重建自己!”?Angular2如何重建视图(路由)

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

回答

1

当您使用不同的参数导航到相同的路由时,它会重用该组件。因此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); 
    }); 
} 

也期待在另一个所以question

+0

嘿嘿,谢谢。它工作但很奇怪。我是一个路由器回来。它就像:Cliked A - > nothing,点击B - >重定向到A,点击C - >重定向到B。你知道什么是错的吗? – elzoy

+0

向我们展示一些代码,您可能会看到控制台表达式上的警告在检查后发生了变化。尝试在setTimeout中包装setter。向我们展示与参数处理相关的代码 –

+0

对不起,我忘了重构我的代码。我在里面订阅this.route.params.snapshot ['id']而不是params ['id']。谢谢,你摇滚;) – elzoy