NG.V · Angular 22 practice question

In Angular, both `CanDeactivate` guards and custom route resolvers can be used to control navigation flow. When using a `CanDeactivate` guard on a component that holds an unsaved form, which is the primary purpose of the guard, and how does it differ from what a route resolver typically achieves?

Choose one answer
Check answer and explanation

Correct answer: `CanDeactivate` prevents navigation by returning `false` or an observable that emits `false`, forcing the user to confirm leaving the component, whereas a route resolver is used to fetch necessary data *before* the component is instantiated.

Explanation

The `CanDeactivate` guard is designed to check the state of the current component (like having unsaved changes) and determine if the user should be allowed to leave that route. It returns `true` (allow), `false` (deny), or an observable/promise that resolves to one of those two values. A route resolver's primary job is to asynchronously fetch and provide necessary data to the component *before* it is initialized, not to block navigation based on the component's internal state.

Common trap

Confusing the preventative nature of `CanDeactivate` (blocking exit) with the preparatory nature of a resolver (fetching input data).

Source: Angular Router Documentation - (Check the specific documentation on Route Guards, as Angular 22 features may refine this distinction) https://angular.dev/guide/routing
Checked against the cited text on 2026-11-27.

Report an error in this question