I'm trying to get parameters from url in AppComponent in my angular application.
But when I use ActivatedRoute to get params it gives null. This is my router configuaration.
{
path: '',
component: HomeComponent,
canActivate: [AuthGuard]
},
{
path: 'product/:product',
component: HomeComponent,
canActivate: [AuthGuard]
}
This is what I tried in AppComponent to get the the result of params from url.
constructor(
private route: ActivatedRoute,
private router: Router
) {}
ngOnInit() {
this.route.params.subscribe(params => {
console.log(params['product']); //jsut print the result in console
});
//or can use to get the result
//console.log(this.route.snapshot.params['product'];
}
this code only works inside particular components. So, Is there any suitable way to get the results. [Angular 6+]