I've been facing an issue with accessing the :id
route parameter in a router guard. It seems to always return an empty Object{}
.
Initially, I was unsure of how to approach this problem, so I referred to this question for guidance. However, it didn't provide a solution as my problem lies within a guard
.
Here's a snippet from my route declaration in app-routing.module
:
const dashboardRoutes: Routes = [
{
path: "dashboard/:id",
component: dashboard.DashboardComponent,
canActivate: [guard.LoggedInGuard],
children: [
{
path: "",
redirectTo: "home",
pathMatch: "full"
},
...
And here's part of my guard code located in existsInDatabase.guard
(I attempted using both params
& queryParams
):
constructor(
private router: Router,
private activatedRoute: ActivatedRoute
) { }
canActivate() {
console.log(this.activatedRoute.params);
this.activatedRoute.params.subscribe(param=> {
console.log(param); // outputs an empty Object{}
console.log(param['id']); // shows undefined
});
this.activatedRoute.queryParams.subscribe(param=> {
console.log(param); // produces an empty Object{}
console.log(param['id']); // gives undefined
});
return true;
}
The issue arises when transitioning from
http://localhost:4200/dashboard/136364285
to:
http://localhost:4200/dashboard/136364285/dock/1654321
How can I successfully retrieve 136364285
?