I want to avoid user change the URL from /dashboard/1234-12
to /dashboard
to prevent an error, so I want to redirect to /home
when user type /dashboard
without the id
.
I have this route:
{
path: 'dashboard/:id',
component: DashboardComponent,
canActivate: [CanActivateViaAuthGuard, CheckVidGuard],
},
And this CheckVidGuard:
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): boolean {
const vid = route?.params?.vid;
if (!vid) {
this.router.navigate(['/home']);
return false;
}
return true;
}
My Dashboard component only get this id
and do some things:
ngOnInit() {
this.id = this.activatedRoute.snapshot.paramMap?.get('id');
}
But every time I tried to go to /dashboard
(without the id param) to get redirected to /home
I got this error Error: NG04002: Cannot match any routes. URL Segment: 'dashboard'
I know that there is no route defined without the id
but the guard seems to not work.
I tried adding this route and it works, but I don't want to add other route for this.
{
path: 'dashboard',
component: DashboardComponent,
canActivate: [CanActivateViaAuthGuard],
},
With a Resolver
the result is the same, don't work:
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): string {
const vid = route.params['vid'];
if (!vid) {
this.router.navigate(['/home/dashboard']);
return null;
}
return vid;
}