0
0
Angularframework~5 mins

Route guards (canActivate, canDeactivate) in Angular - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the canActivate guard in Angular routing?
The canActivate guard controls if a route can be entered. It checks conditions before allowing navigation to a route, like user authentication.
Click to reveal answer
beginner
What does the canDeactivate guard do in Angular?
The canDeactivate guard checks if it is okay to leave the current route. It can prevent navigation away, for example, if a form has unsaved changes.
Click to reveal answer
intermediate
How do you implement a canActivate guard in Angular?
Create a service that implements the CanActivate interface. Add a canActivate() method that returns true or false (or an Observable/Promise of those). Then add this guard to the route's canActivate array.
Click to reveal answer
beginner
When would you use canDeactivate guard in a real app?
Use canDeactivate to warn users about unsaved changes before leaving a form or page. It helps avoid losing data by accident.
Click to reveal answer
intermediate
What types of values can canActivate and canDeactivate return?
They can return a boolean, an Observable<boolean>, or a Promise<boolean>. This allows synchronous or asynchronous checks before navigation.
Click to reveal answer
What does the canActivate guard control?
AWhether a route can be entered
BWhether a route can be left
CHow a route is styled
DHow data is fetched for a route
Which interface should a service implement to act as a canDeactivate guard?
ACanActivate
BResolve
CCanDeactivate
DCanLoad
What can canActivate return to allow asynchronous checks?
AObservable<boolean> or Promise<boolean>
BOnly string
CNumber
DOnly boolean
When is canDeactivate guard typically used?
ABefore entering a route
BBefore leaving a route
CTo fetch data
DTo style components
How do you add a canActivate guard to a route?
AAdd it to the module imports
BAdd it to the route's <code>canDeactivate</code> array
CAdd it to the component's decorator
DAdd it to the route's <code>canActivate</code> array
Explain how canActivate and canDeactivate guards help control navigation in Angular apps.
Think about when you want to stop users from going to or leaving a page.
You got /4 concepts.
    Describe the steps to create and use a canActivate guard in an Angular application.
    Focus on the service, method, return type, and route setup.
    You got /4 concepts.