Search code examples
javascriptangulartypescriptrxjsangular-router

Subscribing to paramMap breaks routing example?


When switching from params to paramMap in this stackblitz hello component routing example the routing no longer displays the correct result in the router outlet.

I changed the original ngOnInit code in hello-component.ts to this:

ngOnInit() {
   this.route.paramMap.subscribe(params =>
     this.name = params.get['name']
   )
 }

But still no love. I have enable tracing turned on for the router. Is there a way to see what the problem is?


Solution

  • Parammap returns a map and not an object so you have to change your code to like this

    this.name = params.get('name') to get name param from the map.

    Check updated code here

    Check this link for more information on how to use paramMap https://angular.io/api/router/ParamMap