canActivate guard that returns false. What is the behavior when a user tries to navigate to this route?If a canActivate guard returns false, Angular cancels the navigation. The user remains on the current page and the new route does not load.
canDeactivate guard checks a flag isSaved. If the guard returns true, what can we say about the isSaved flag after navigation away?class EditComponent { isSaved = false; // canDeactivate guard checks this flag }
The canDeactivate guard returning true means navigation is allowed. It does not guarantee that isSaved is true. The flag remains as it was.
canActivate guard method that returns an Observableimport { 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 } }
The canActivate method must return an Observableof(this.isLoggedIn) creates an Observable emitting the boolean value.
Option C returns a boolean, not an Observable. Option C returns a Promise, not Observable. Option C uses deprecated syntax.
canDeactivate(component: any): boolean {
return component.isSaved && component.hasPermission();
}class EditComponent { isSaved = true; // hasPermission method is missing }
The guard calls component.hasPermission(), but the component does not have this method. This causes a TypeError at runtime.
canActivate guard returns a UrlTree instead of true or false, what happens?Returning a UrlTree from a canActivate guard tells Angular to cancel the current navigation and redirect to the URL represented by the UrlTree.