/ angular

Angular read param from URL

Well the below is a hack that seems to work in stackblitz, however it didn't work in my app so this is the solution that works.

export class StoresComponent implements OnInit {

  id: string;

  constructor(private route: ActivatedRoute) {

  ngOnInit() {
    this.id = this.route.snapshot.params.store;


end update

Spent a long time reading the example and API doc

Turns out you can just use route.params.value.id;

Didn't have to mess with Observable.

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, ParamMap } from '@angular/router';
import { Observable } from 'rxjs/Observable';

  selector: 'my-app',
  template: `<h1>page 2 {{id}} ok</h1>`,
export class PageComponent implements OnInit  {
  //id: Observable<string>;
  id = '';

    route: ActivatedRoute
  ) {
    this.id = route.params.value.id;

For context, path declaration in app.module.ts:

export const routes: Routes = [
    path: '',
    component: AppComponent,
    path: '2/:id',
    component: PageComponent,
Angular read param from URL
Share this