How to set and fetch specific values for route params in Angular

582 Views Asked by At

I have a set of routes like this:

/users/:type/:id
/users/admin/:id
/user/manager/:id

Now all of these use the same component. The catch is, admin and manager can see some extra stuff based on their type. Assuming I don't have a way to get these from user roles and then show the relevant information, can I use the type route param to identify if the user is admin, manager or any other type of user?

Essentially, in my init function, I am setting something like this:

this.userType = params.userType;

This will work in case of first route as we have a param set for type. Can I tell angular that the value admin and manager are actually the value of param type?

1

There are 1 best solutions below

2
Chinmaya Sahoo On

you can use activatedroute to get the query params

first in the constructor you have to declare activateroute something like this

constructor(private activatedRoute: ActivatedRoute){}

Import activated route and then on ngonit you write the following code to get the values

this.activatedRoute.queryParams.subscribe(params=>{
    this.user=params.type;
    this.id=params.id
})