sancho21 sancho21 - 2 years ago 106
Javascript Question

Why importing operator in RxJs is needed?

While following an Angular tutorial, I found this:

import 'rxjs/add/operator/switchMap';

export class HeroDetailComponent implements OnInit {
ngOnInit(): void {
this.route.paramMap
.switchMap((params: ParamMap) => this.heroService.getHero(+params.get('id')))
.subscribe(hero => this.hero = hero);
}
}


Sorry if I found this a bit strange (due to 2 reasons: I came from Java and I'm not really following latest JS technology). Isn't
switchMap
a method owned by
param map
which an
Observable<ParamMap>
? When I remove the import statement, the code doesn't compile.

I use RxJava, but all methods required to manipulate an observable are already there (attached) with the Observable class itself.

Can someone tell me, why
switchMap
import is required? Possibly, give me some reference links.

Answer Source

you can read this article : Where are my Observable operators

at lot of operators are missing. But this is not a fault, this is by design. Angular will not ship all available operators. This would result in additional 300 kb. So, to get an Observable with more operators, you could either import the ones you need, or import all.

when you build web application you should use less http requests, and for better performance you should compress your file size.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download