Q: Angular2: 'switchMap' does not exist on type 'Observable<Params>'
You need to import the switchMap
operator:
import 'rxjs/add/operator/switchMap';
update for rxjs >=5.5.0:
for [email protected] or higher it's recommended to use the pipe operator instead of augmentation:
import { switchMap } from 'rxjs/operators';
\\...
this.route.params.pipe(switchMap((params: Params) => /*... */))
.subscribe(/*... */);
\\...
(this avoids side effects and enable better bundle size optimization)
import { Observable, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
this.user = this.afAuth.authState.pipe(switchMap(user => {
if (user) {
return this.afs.doc<User>(`users/${user.uid}`).valueChanges()
} else {
return of(null)
}
}));
}
This issue has been fixed, check below:
this.route.params.forEach((params: Params) => {
if (params['id'] !== undefined) {
let id = +params['id'];
this.heroService.getHero(id)
.then(hero => this.hero = hero);
}
});