Czytam Angular Guide about Routing & Navigation.Angular 2: dlaczego używać switchMap podczas pobierania parametrów trasy?
one wykorzystać ten kod do pobierania routera param 'id'
i używając go dostać bohatera z usługą service
:
ngOnInit() {
this.route.params
.switchMap((params: Params) => this.service.getHero(+params['id']))
.subscribe((hero: Hero) => this.hero = hero);
}
Ale nie dobrze zrozumieć, co jest celem za pomocą operatora switchMap
w powyższy kod.
Poniższy kod nie byłby taki sam?
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)
});
}