rexar1988
9/20/2018 - 1:46 PM

Angular: Url params

Angular: Url params

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

@Component({
  selector: 'app-adv',
  templateUrl: './adv.component.html',
  styleUrls: ['./adv.component.scss']
})
export class AdvComponent implements OnInit {
  id: number;

  constructor(private _route: ActivatedRoute) { }

  ngOnInit() {
    this.id = this._route.snapshot.params['id'];
  }
}
<p>adv works!</p>
<p>You advert is {{ id }}</p>
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { AdvComponent } from './adv.component';

const routes: Routes = [
  {
    path: ':id', component: AdvComponent
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class AdvRoutesModule {}