Noble Eugene
Noble Eugene

Reputation: 737

Angular route param is empty

I have a routerlink that looks like this:

  <li class="list-group-item d-flex justify-content-between align-content-center" *ngFor = "let union of activeUnions" [routerLink] = "['union',union.id]">

Which is routing using the route:

const routes: Routes = [
  {
    path: '',
    component: IndexPage
  },
  {
    path:"new",
    component:NewPage
  },
  {
    path:"union/:id",
    component:UnionPage
  }
];

This is sending me to the required component but when i try to get the id like such


  ngOnInit() {
    let id = this.$common.$route.snapshot.paramMap
    console.log(id);
  }

I am getting an empty object instead of an object containing the id from the url, whats is wrong?

Upvotes: 0

Views: 1581

Answers (1)

See if you can get the desired id with this:

...
import { ActivatedRoute } from '@angular/router';
...

constructor(
   ...
   private activatedRoute: ActivatedRoute
   ...
) {}


 ngOnInit(): void {
    const id = Number(this.activatedRoute.snapshot.paramMap.get('id'));
    console.log(id);
  }

Upvotes: 1

Related Questions