我在读Angular Guide about Routing & Navigation.
他们使用此代码检索路由的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)
});
}