
Angular Guards
This covers Angular route guards with a focus on the modern functional approach introduced in recent versions. You'll want this when protecting routes with CanActivate, preventing navigation with CanDeactivate, or using CanMatch to block lazy loading for unauthorized users. The examples show the shift from class-based guards to functional ones using inject(), which is now the recommended pattern. It includes practical scenarios like checking auth status, protecting child routes, and handling unsaved form data. The guard chaining and data passing examples are solid if you're building anything beyond basic authentication. Worth noting it assumes you're on Angular 21 or at least comfortable with the standalone API.
- skills.sh installs
- 184
- GitHub stars
- 7
- last updated
- Aug 21, 2026
Install
$ npx -y skills add oguzhan18/angular-ecosystem-skills --skill angular-guards --agent claude-codeInstalls into .claude/skills of the current project.
Files
Angular Route Guards
Version: Angular 21 (2025) Tags: Guards, Routing, Auth, CanActivate
References: Guards Guide • CanActivate
API Changes
This section documents recent version-specific API changes.
-
NEW: Functional guards — Use
CanActivateFninstead of class-based -
NEW: CanMatch guard — Prevent lazy loading of unauthorized code
-
NEW: provideRouter with guards — Modern guard registration
-
DEPRECATED: Class-based guards — Migrate to functional
Best Practices
- Create functional guard
export const authGuard: CanActivateFn = () => {
const authService = inject(AuthService);
const router = inject(Router);
if (authService.isAuthenticated()) {
return true;
}
return router.createUrlTree(['/login']);
};
- Use CanActivate for route protection
const routes: Routes = [
{
path: 'dashboard',
canActivate: [authGuard],
component: DashboardComponent
}
];
- Use CanActivateChild for child routes
Sponsors
Details
- Repository
- github.com/oguzhan18/angular-ecosystem-skills
- Category
- Frontend Development
- First seen
- Jun 3, 2026
More from oguzhan18/angular-ecosystem-skills
All 45- Angular Interceptors184
- Angular Schematics184
- Angular Components182
- Angular Destroyref182
- Angular Forms182
- Angular Pipes180
- Angular Resolvers179
- Angular Deferrable178
- Angular Resource178
- Angular Signals178
- Angular Signals Forms178
- Angular Reactive177
- Angular Directives176
- Angular Lifecycle176
- Angular Di175
- Angular Viewchild174
- Angular Cypress172
- Angular Router172
- Angular Control Flow170
- Angular Zoneless170
- Nx170
- Angular Signals Http169
- Angular Ssr168
- Angular Injector165