0
0
Angularframework~20 mins

Route guards (canActivate, canDeactivate) in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular Route Guard Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when canActivate guard returns false?
Consider an Angular route with a canActivate guard that returns false. What is the behavior when a user tries to navigate to this route?
AThe navigation proceeds and the route component loads normally.
BThe navigation is cancelled and the user stays on the current page.
CThe application throws a runtime error and crashes.
DThe router redirects automatically to the root path.
Attempts:
2 left
💡 Hint
Think about what a guard returning false means for navigation.
state_output
intermediate
2:00remaining
What is the value of a flag after canDeactivate guard returns true?
In an Angular component, a canDeactivate guard checks a flag isSaved. If the guard returns true, what can we say about the isSaved flag after navigation away?
Angular
class EditComponent {
  isSaved = false;
  // canDeactivate guard checks this flag
}
AisSaved is false, meaning changes are not saved but navigation allowed.
BisSaved is true, meaning changes are saved before leaving.
CisSaved is undefined because the guard does not affect it.
DisSaved is reset to null automatically by Angular.
Attempts:
2 left
💡 Hint
The guard returning true means navigation is allowed, but does it guarantee saving?
📝 Syntax
advanced
2:30remaining
Which option correctly implements a canActivate guard returning an Observable?
Select the correct Angular canActivate guard method that returns an Observable allowing navigation only if a user is logged in.
Angular
import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router';
import { Observable, of } from 'rxjs';

@Injectable({ providedIn: 'root' })
export class AuthGuard implements CanActivate {
  isLoggedIn = false;

  canActivate(): Observable<boolean> {
    // fill in correct return
  }
}
Areturn new Promise(resolve => resolve(this.isLoggedIn));
Breturn this.isLoggedIn ? true : false;
Creturn of(this.isLoggedIn);
Dreturn Observable.of(this.isLoggedIn);
Attempts:
2 left
💡 Hint
Use RxJS to return an Observable of a boolean value.
🔧 Debug
advanced
2:30remaining
Why does this canDeactivate guard cause a runtime error?
Given this canDeactivate guard code, why does it cause a runtime error when navigating away?
canDeactivate(component: any): boolean {
  return component.isSaved && component.hasPermission();
}
Angular
class EditComponent {
  isSaved = true;
  // hasPermission method is missing
}
AThe guard returns false always, blocking navigation.
Bcomponent.isSaved is undefined, causing a ReferenceError.
CThe guard returns a Promise instead of boolean, causing error.
Dcomponent.hasPermission is not a function, causing a TypeError.
Attempts:
2 left
💡 Hint
Check if all methods used on component exist.
🧠 Conceptual
expert
3:00remaining
What is the effect of returning a UrlTree from canActivate guard?
In Angular routing, if a canActivate guard returns a UrlTree instead of true or false, what happens?
AThe router cancels current navigation and redirects to the UrlTree path.
BThe router ignores the UrlTree and proceeds with navigation.
CThe router throws a syntax error because UrlTree is invalid return type.
DThe router navigates to both the original and UrlTree paths simultaneously.
Attempts:
2 left
💡 Hint
Think about how Angular handles redirect instructions from guards.