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-code

Installs 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 CanActivateFn instead 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
1oguzhan18 avatarangular-interceptorsoguzhan18/angular-ecosystem-skillsALWAYS use when working with Angular HTTP Interceptors, request/response transformation, auth interceptors, or error handling in HTTP calls.184skills.sh installs
2oguzhan18 avatarangular-schematicsoguzhan18/angular-ecosystem-skillsALWAYS use when working with Angular Schematics, custom generators, code generation, or building CLI tools in Angular.184skills.sh installs
3nexu-io avatarhtml-ppt-tech-sharingnexu-io/open-designConference / internal tech-talk deck — GitHub-dark, JetBrains Mono, terminal code blocks, agenda + Q&A pages. Use for engineering presentations, internal sharin183skills.sh installs
4nexu-io avatarhtml-ppt-presenter-modenexu-io/open-design演讲者模式专用 deck — tokyo-night 默认主题,5 套主题 T 键切换,每页带 150-300 字逐字稿示例(<aside class="notes">),按 S 打开 popup(CURRENT / NEXT / SCRIPT / TIMER 四张磁吸卡片)。用于技术分享、公开演讲、课程讲解,怕忘词或182skills.sh installs
5nexu-io avatarhtml-ppt-testing-safety-alertnexu-io/open-design红琥珀警示 deck — 顶/底 45° 红黑 hazard 条纹、红色删除线否定标题、L1/L2/L3 绿/琥珀/红 tier 卡片、圆点状态 alert box、policy-yaml 代码块(红左边框 + bad 关键词高亮)、红绿 checklist、Q1 事故堆叠柱状图。适合安全 / 风险 / 事故复盘 / 182skills.sh installs
6oguzhan18 avatarangular-formsoguzhan18/angular-ecosystem-skillsALWAYS use when working with Angular Forms, reactive forms, template-driven forms, form validation, or form handling in Angular applications.182skills.sh installs