2016-07-13 42 views
9

我正在尝试导航到具有不同id值的同一页面。因此,如果我位于/test/1并转至/test/2,浏览器中的网址会更新,但视图不会刷新。我调试了ngOnInit,导航到/test/2时没有重新运行。但是,如果我从test/1other路由工作正常,只有导航到具有不同参数的相同路由时才会出现问题。有没有其他人遇到过这个?当我得到一些时间不适时上传一个plunkr。角rc3路由器 - 导航到具有不同参数的相同页面

角2 RC3路由器3.0.0-beta.2

RouterConfig = [ 
    { 
     path: '', 
     component: 'Layout', 
     children: [ 
      { 
       path: 'test/:id', 
       component: TestComponent 
      }, 
      { 
       path: 'other', 
       component: OtherComponent 
      } 
     ] 
    } 
] 

感谢, LL

回答

14

当您导航到不同的PARAM相同的路线来重用的组件。因此ngOnInit将不会再被调用。 您应该订阅在ngOnInit到routeparam然后执行视图更新的订阅功能

在构造

进样活性路线

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

ngOnInit方法,我们用ActivatedRoute服务检索我们的路线参数

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

有关详细信息see和部分 “获取路线参数”

+0

好吧,我明白了。我的问题是我在ngOnInit中的服务调用,而不是我的params订阅。谢谢Agarwal – kLai

相关问题