1

I'm new in Angular and i'm blocked since two days. The command "const pokemonId: string|null = this.route.snapshot.paramMap.get('id');" returns null to pokemonId but normally it gives me the string from the URL (http://localhost:4200/pokemon/4) like here "4"

detail-pokemon.component.ts :


import { Component, OnInit } from '@angular/core';
import { ActivatedRoute} from '@angular/router';
import { POKEMONS } from '../mock-pokemon-list';
import { Pokemon } from '../pokemon';

@Component({
  selector: 'app-detail-pokemon',

  templateUrl: './detail-pokemon.component.html',
  
})


constructor (private route: ActivatedRoute,  ){}
ngOnInit()  {

this.pokemonList = POKEMONS;

 const pokemonId: string|null = this.route.snapshot.paramMap.get('id');

 if (pokemonId) {
 
 this.pokemon = this.pokemonList.find(pokemon => pokemon.id == pokemonId)}}}

app-routing.module.ts
const routes: Routes = [
  { path: 'pokemons',component: ListPokemonComponent },
  {path : 'pokemon/:id ', component: DetailPokemonComponent},
  {path: '', redirectTo : 'pokemons', pathMatch: 'full'}
];

I tried with getAll and all proposed solutions for situation looking like mine .

Wilt
  • 41,477
  • 12
  • 152
  • 203
Mehdi332
  • 11
  • 1

2 Answers2

1

Instead of using paramsMap try this

import {ActivatedRoute} from '@angular/router';
    
    constructor(

        private route: ActivatedRoute

      ) {}
    
    **const pokemonId: string|null=this.route.snapshot.params.id**
HARIDHASS
  • 11
  • 3
1

You need to remove the space after :id inside your path. So change:

'pokemon/:id '
            _

To:

'pokemon/:id'
Wilt
  • 41,477
  • 12
  • 152
  • 203