Video Course

Routing Angular v2+ Applications: Lesson 9 of 23

Route Parameters

Up Next

Refactoring for User Service

Autoplaying in 7 seconds!

Cancel

To get route parameters, we'll be using something called the ActivatedRoute from the @angular/router.

Once we have the ActivatedRoute, we can grab params using:

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

...

export class AboutUserComponent implements OnInit {
  user;

  constructor(private route: ActivatedRoute) {}

  ngOnInit() {
    // grab the current username
    let username = this.route.snapshot.params['username'];

    this.user = users.find(function(user) {
      return user.username === username;
    });
  }

}

Chris Sevilleja

162 posts

Co-founder of Scotch.io. Slapping the keyboard until something good happens.