42
我正在閱讀Angular Guide about Routing & Navigation。Angular 2:爲什麼在檢索路由參數時使用switchMap?
他們使用此代碼檢索路由器的PARAM 'id'
並用它來得到一個英雄與service
服務:
ngOnInit() {
this.route.params
.switchMap((params: Params) => this.service.getHero(+params['id']))
.subscribe((hero: Hero) => this.hero = hero);
}
但我不能很好地理解什麼是在使用switchMap
運營商的目的以上代碼。
以下代碼將不會相同?
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)
});
}