I have these two guards
@Injectable({
providedIn: 'root'
})
export class DesktopGuard implements CanActivate {
constructor(
private router: Router,
private route: ActivatedRoute,
) { }
canActivate() {
if (window.innerWidth < 768) {
this.router.navigate(['./mobile'], {relativeTo: this.route.parent});
return false;
}
return true;
}
}
@Injectable({
providedIn: 'root'
})
export class MobileGuard implements CanActivate {
constructor(
private router: Router,
private route: ActivatedRoute,
) { }
canActivate() {
if (window.innerWidth > 768) {
this.router.navigate(['./welcome'], {relativeTo: this.route.parent});
return false;
}
return true;
}
}
And these routes.
const routes: Routes = [
{
path: '',
redirectTo: 'grid/welcome',
pathMatch: 'full',
},
{
path: 'grid',
component: GridContainerComponent,
children: [
{
path: 'mobile',
canActivate: [MobileGuard],
component: WelcomeMobileComponent,
},
{
path: 'welcome',
canActivate: [DesktopGuard],
component: WelcomeComponent
},
]
},
];
Guard should prevent the component from loading and redirect based on the window size. My issue is the redirect to ./mobile
, it keeps re-running the guard and consuming memory, i.e it will not redirect to WelcomeMobileComponent
. I suspect the router is to blame, but I am not sure.
Any ideas?
thanks!