I have a little webapp that basically lists a player list and when user clicks on the player name, it shows player details. It uses two api calls, one for list and other to load details of selected player. My problem is that when i go back from details route to home route the component executes ngOnInit again so it makes again the first api call to load the home list again, and i don't want this call again. This is my configuration:
routing.module.ts
...
export const routes: Routes = [
{
path: '',
component: BioStatsComponent,
resolve: {
biostats: BiostatsResolverService
}
},
{
path: 'info',
children: [
{
path: ':id',
component: InfoComponent,
resolve: {
info: PlayersResolverService
}
}
]
}
];
...
detail.component.ts
ngOnInit(){
...
this.playersinfo = this.route.snapshot.data['info'];
...
}
home.component.ts
ngOnInit(){
...
this.biostats$ = this.route.snapshot.data['biostats'];
...
}
detail-resolver.service.ts
...
resolve(route: ActivatedRouteSnapshot, rstate: RouterStateSnapshot): Observable<any> {
const id = route.paramMap.get('id');
return this.playerService.getPlayerCommonInfo(+id).catch((err: any) => of([err]));
}
...
home-resolver.service.ts
constructor(private playerService: PlayersService) {}
resolve(route: ActivatedRouteSnapshot, rstate: RouterStateSnapshot): Observable<any> {
return this.playerService.getPlayerBioStats();
}