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?✗ Incorrect
canActivate controls if navigation to a route is allowed.
Which interface should a service implement to act as a
canDeactivate guard?✗ Incorrect
The CanDeactivate interface is used for guards that check if a route can be exited.
What can
canActivate return to allow asynchronous checks?✗ Incorrect
It can return an Observable or Promise of boolean to handle async logic.
When is
canDeactivate guard typically used?✗ Incorrect
canDeactivate runs before leaving a route to confirm navigation.
How do you add a
canActivate guard to a route?✗ Incorrect
Guards are added to the route configuration under canActivate.
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.