user1765862
user1765862

Reputation: 14145

Fetching param in angular2

My goal is to fetch username from a url

Inside app.routing I have route export const routes: Routes = [
{ path: 'dashboard/:username', component: App } ];

inside appp component I'm trying to fetch this username using

let username = this.route.snapshot.queryParams["username"]; 

My browser is using localhost:91/dashboard/john

username is always undefined.

Upvotes: 0

Views: 33

Answers (3)

Léo R.
Léo R.

Reputation: 2698

Try this :

this.route.snapshot.params['username']

Instead of queryParams

Upvotes: 0

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48357

You don't need to use queryParams here. This is useful when you have something like this:

localhost:91/dashboard?username=john

Just use params property.

this.route.snapshot.params["username"]; 

I suppose you declared route in following way:

{ path: 'dashboard/:username', component: SomeComponent}

Upvotes: 1

Aravind
Aravind

Reputation: 41573

You should be using it as params

let username = this.route.snapshot.params["username"]; 

Upvotes: 1

Related Questions