My app renders a project component which contains information of a given ID from the URL like my.app/project/foo
.
The app component:
<router-outlet>
to render the project componentThe problem is: When you click a link, the route correctly changes with the project id, but the project component does not re-render again based on the new id. After reloading the project component renders correctly, but not after clicking on another id.
app.routing.module.ts:
const routes: Routes = [
...
{
path: 'project/:key',
loadChildren: () =>
import('../project-page/project-page.module').then(
m => m.ProjectPageModule
)
}
...
];
app.component.html:
<a routerLink="/project/{{ project.id }}" *ngFor="let project of projects">
{{ project.name }}
</a>
...
<div class="content">
<router-outlet></router-outlet>
</div>
project-page.component.ts (within ProjectPageModule):
export class ProjectPageComponent implements OnInit {
project: any;
ngOnInit(): void {
const id = this.route.snapshot.paramMap.get('id');
return this.projectService
.getProjectById(id)
.pipe(delay(1000) /* debug only */)
.subscribe(response => this.project = response);
);
}
...
}
project-page.component.html:
<h1>{{ project.id }}</h1>
I'm not sure if
What do you think?
The solution I came up with was that the child component needs to listen to route changes in its ngOnInit
method:
ngOnInit(): void {
this.route.params.subscribe((param) => {
this.getProjectFromUrl(param.id);
});
}
getProjectFromUrl(id: number): any {
return this.projectService
.getProjectById(id)
.pipe(delay(1000) /* debug only */)
.subscribe(response => this.project = response);
);
}
Yay!