Home > Software engineering >  Is there a way to do NodeJs Express style Parameters in Angular?
Is there a way to do NodeJs Express style Parameters in Angular?

Time:08-24

I am wondering if there is a way to have a Parameter in Angular route that is similar to NodeJs Style ?

i have a route which looks like this "**http://myhost.domain.com/signin**" and i want to be able to pass an id for the signin request. I know i can do it like this "http://myhost.domain.com/signin?id=123456", but i am looking to do it more like a nodejs style where it would look like this http://myhost.domain.com/signin/123456

currently i read the params like this, but when i use the above url it cant find it since it thinks its one level deeper route.

    ngOnInit() {
            this.route.queryParams
                .subscribe(params => {
            console.log(params); 
    
          }
        );

CodePudding user response:

Yes, you can do it. First, you need to configure the route in the Router:

const routes: Routes = [
  { path: 'signin/:id', component: SignInComponent }
]

Then, you can access it in the component like this:

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

...

constructor(private route: ActivatedRoute) {}

...

ngOnInit() {
  const id = this.route.snapshot.params.id;
}
  • Related