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"]);
};

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.

Further reading

Problem with a question

Angular · Middle · question 29

Test yourself

Take the Angular · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz