Skip to main content

Route guards and protected routes

Learn Route guards and protected routes (Module 10 · Angular Routing, Angular) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

Route guards: the bouncers of your app • A route guard decides whether a navigation may continue. Like a security guard at a door it returns true (come in), false (stay out), or a UrlTree (go there instead, e.g. the login page). • CanActivate : may I enter this route? CanActivateChild : may I enter any child? CanDeactivate : may I leave (unsaved changes)? CanMatch : should this route even be considered? • Modern guards are functional route guards : plain functions ( CanActivateFn ) that use inject() . Class-based guards are legacy and deprecated.

More lessons in Module 10 · Angular Routing