0

How get variable from url path variable in angular2(v12)

 path: 'shop/:id', <-- get id from here
 component: RedirectGuard,
  canActivate: [RedirectGuard],
  data: {
    externalUrl: `https://example.com/shop/:id` <-- set id to here
  }

1 Answers1

0

You want to use the ActivateRoute class in your component:

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

export class RedirectGuard implements OnInit {
 constructor(
        private route: ActivatedRoute
    ) {}

 ngOnInit() {
   const id = this.route.snapshot.params.id;
   this.router.navigate([`shop`, id]);
 }
}
Tom Slabbaert
  • 21,288
  • 10
  • 30
  • 43