I'm reading the Angular Guide about Routing & Navigation.
They use this code for retrieving the router's param 'id'
and using it to get a hero with the service
service:
ngOnInit() {
this.route.params
.switchMap((params: Params) => this.service.getHero(+params['id']))
.subscribe((hero: Hero) => this.hero = hero);
}
But I do not well understand what is the purpose of using the switchMap
operator in the above code.
The following code would not be the same?
ngOnInit() {
this.route.params
// NOTE: I do not use switchMap here, but subscribe directly
.subscribe((params: Params) => {
this.service.getHero(+params['id']).then(hero => this.hero = hero)
});
}