2

I'm trying to have a route called 'event' with parameter event_id in the URL.

app routing module has this

{ path: 'event/:event_id', component: EventComponent },

The component tries to get event_id from url using Activated route.

import { ActivatedRoute, Params } from '@angular/router';
export class EventComponent implements OnInit {
  event: JSON;
  id: String;
  constructor(private route: ActivatedRoute) {
  }
  ngOnInit() {
    this.route.queryParams.subscribe(params => {
      console.log('that kind of madness can\'t be undone');
      console.log(params['event_id']); // checking result
    })
  }

console.log(params['event_id']) is giving an empty object as a result.

Flyn Sequeira
  • 718
  • 2
  • 9
  • 25
  • you are using `routeParams` and if you need to use `queryParams` you should have relative route definition – Aravind Dec 12 '17 at 18:38
  • Possible duplicate of [How to get parameter on Angular2 route in Angular way?](https://stackoverflow.com/questions/40275862/how-to-get-parameter-on-angular2-route-in-angular-way) – Jota.Toledo Dec 12 '17 at 18:46
  • There are three different ways to use routing parameters and the syntax for each one is a bit different. So it's important to understand which type of parameter you need and use all of the syntax appropriate for that type. See a summary of the types and syntax here: https://stackoverflow.com/questions/44864303/sending-data-with-route-navigate-in-angular-2/44865817#44865817 – DeborahK Dec 12 '17 at 20:07

1 Answers1

2

:eventId isn't a query parameter, it would belongs to route parameter, so check params Observable on current activate route.

this.route.params.subscribe(params => {
  console.log('that kind of madness can\'t be undone');
  console.log(params['event_id']); // checking result
})
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299