• Overview
@angular/router

RedirectCommand

Class
stable

Can be returned by a Router guard to instruct the Router to redirect rather than continue processing the path of the in-flight navigation. The redirectTo indicates where the new navigation should go to and the optional navigationBehaviorOptions can provide more information about how to perform the navigation.

API

    
      class RedirectCommand extends Error {}
    
    

constructor

RedirectCommand
@paramredirectToUrlTree
@paramnavigationBehaviorOptionsNavigationBehaviorOptions | undefined

name

string

message

string

stack

string | undefined

cause

unknown

zoneAwareStack

string | undefined

Stack trace where extra frames have been removed and zone names added.

originalStack

string | undefined

Original stack trace with no modifications

Description

Can be returned by a Router guard to instruct the Router to redirect rather than continue processing the path of the in-flight navigation. The redirectTo indicates where the new navigation should go to and the optional navigationBehaviorOptions can provide more information about how to perform the navigation.

const route: Route = {
  path: "user/:userId",
  component: User,
  canActivate: [
    () => {
      const router = inject(Router);
      const authService = inject(AuthenticationService);

      if (!authService.isLoggedIn()) {
        const loginPath = router.parseUrl("/login");
        return new RedirectCommand(loginPath, {
          skipLocationChange: true,
        });
      }

      return true;
    },
  ],
};
Jump to details