yogitha c
yogitha c

Reputation: 121

Access dynamic URL param in angular 6

In my angular 7 code I'm trying to fetch the clientId passed in the URL. clientId will be dynamic.

localhost:4200/client/xyz

app.routing.ts

 {
  path: 'client/:clientId',
  component: AppComponent
 },

app.component.ts

constructor(private route: ActivatedRoute) { }

ngOnInit() {
   console.log(this.route.snapshot.paramMap.get('clientId'));
});

It prints null in the console

Upvotes: 2

Views: 1200

Answers (2)

MonkeyScript
MonkeyScript

Reputation: 5121

Use params instead of paramMap,

console.log(this.route.snapshot.params['clientId']);

Upvotes: 0

Tony
Tony

Reputation: 20102

You can get the route params using this method

this.route.params.subsribe(params => {
  console.log(params['clientId'])
});

or

this.activateRoute.snapshot.params['clientId']

PLease let me know if you still have problem

Upvotes: 3

Related Questions