What are route guards? Give examples.

Angular routing · Freshers and experienced

Model answer

A guard decides whether navigation is allowed. Common types: canActivate (can the user enter this route?), canActivateChild, canDeactivate (warn about unsaved changes when leaving) and canMatch (should this route be considered at all, useful for role-based lazy routes).

In current Angular they are plain functions, using inject() to reach services: an auth guard returns true, or a UrlTree that redirects to the login page. Remember that guards protect the screen only. The server must still check permissions.

Tips

Learn this topic

Angular Tutorial: Beginner to Advanced .

Sign in to add your own answer.

More questions