0

I'm trying to get a param in my route, but when i try to print it, is returning undefined in my console.

This is my routing module:

const routes: Routes = [
  {
    path: ':id',
    component: ListaDadosPvSelecionadoComponent,
    canActivate: [AuthGuard],
  },
];

this is how i'm trying to get the params:

ngOnInit() {
    this.activatedRoute.queryParams.subscribe((params) => console.log(params['id']));
}

when i go to route/3 my component is being rendered but i get:

undefined

Why?

veroneseComS
  • 653
  • 2
  • 14
  • 40

2 Answers2

5

params and queryParams are two different things.

Params are part of URL /page/123

Query params are GET parameters /page?id=123

ngOnInit() {
    this.activatedRoute.params.subscribe((params) => console.log(params.id));
}
Bojan Kogoj
  • 5,321
  • 3
  • 35
  • 57
1

Query param works for /domain?id=value

You can use snapshot.paramMap for routes param .

   this.activatedRoute.snapshot.paramMap.get('id')
mr. pc_coder
  • 16,412
  • 3
  • 32
  • 54