Getting error when trying to use paramMap to get route parameters

export class HeroDetailComponent {
    heroes;
    constructor(private route: ActivatedRoute, private router: Router, private hs : HeroService){
    }
    ngOnInit(){
        this.route.paramMap
            .switchMap((params : ParamMap) => 
                this.hs.getHeroes(params.get('id')))
            .subscribe((heroes) => {
                console.log("checking for heroes n subscribe",heroes);
                this.heroes = heroes})          
    }

}

      

Getting next error

paramMap does not exist in type "ActivatedRoute"

+3
angular


source to share


5 answers


import 'rxjs/add/operator/switchMap';



add this import to your component ...

+1


source to share


In your switchmap

paramMap parameter is wrong.

It should be



.switchMap((params : ParamMap) => 

      

0


source to share


not too sure if this helps, but you can try this:

  • delete the node_modules folder in your project
  • in your package.json file make sure your @ angular / router dependency is from angular 4 version (i.e. 4.0.0 and up)
  • make sure your typescript version is at least 2.1.6 in both "dependencies" and "devDependencies" in your package. json (some versions of typescript and up breaks rxjs. I'm not too sure if the versions are higher, that is, to be safe, you can just update your rxjs to the latest version).
  • finally install npm

hope this works for you. I had the same problem and if I'm never wrong I did the above and ParamMap exists after.

0


source to share


Make sure you import ParamMap from @ angular / router

import { Router, ActivatedRoute, ParamMap } from '@angular/router';

      

0


source to share


ParamMap was introduced in version 4.0.0-rc.6. Make sure you have Angular 4 version.

0


source to share







All Articles
Loading...
X
Show
Funny
Dev
Pics