Angular · Middle · Question 29 of 75
What does a functional guard return to redirect the user?
export const authGuard: CanActivateFn = () => {
const auth = inject(AuthService);
const router = inject(Router);
return auth.isLoggedIn() ? true : router.createUrlTree(["/login"]);
};- ✓Correct answer: A
UrlTree(orRedirectCommand), which cancels the navigation and redirects - 2
falsetogether withwindow.location.href - 3It throws an error, which the router turns into a redirect
Explanation
Guards may return a boolean, UrlTree, RedirectCommand, or an Observable/Promise of these. Returning a UrlTree avoids racing navigations that happen when you call navigate inside a guard.