Angular 2 reload component based on activated route

核能气质少年 提交于 2019-12-12 04:34:36

问题


I have a component which I want to re use for different router links and to reload it with server data based on the link param. From what I've seen at the moment it is suggested to have your get inside an ng zone in order to trigger the re-render of the components and its children.

I am also passing the object from the http request further down to child components.

This is the component onInit code:

  ngOnInit(): void {
    this.route.params.forEach((params: Params) => {
        let id = +params['id'];
        this._userHttpService.getUserData(id)
            .subscribe(UserData=> 
                this._ngZone.run(() => this.UserData= UserData)
            );
    });
}

And this is the mark-up

<div *ngIf="UserData">  
    <div class="container-fluid">
        <h3>{{UserData.Id}}</h3>
        <user-tiles [UserData]="UserData"></user-tiles>         
    </div>
</div>

Routes work fine, the correct data is retrieved from the server and the contents inside the <h3>get updated after a second but the "user-tiles" components does not re-render. UserData is an @Input inside user-tiles.

来源:https://stackoverflow.com/questions/40467497/angular-2-reload-component-based-on-activated-route

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