↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

Angular Routing Essentials: All You Need to Know in One Post

The post explains how Angular routing lets an application render different components for different URLs instead of relying only on the component tree. It shows how routes are registered, how router-outlet renders routed content inside the layout, and how routerLink and routerLinkActive handle navigation and active states. It also covers route order, dynamic and query params, redirects, nested routes, programmatic navigation, lazy loading, guards, and resolvers.

Reading notes
#

  • Angular apps render from top to bottom starting with AppComponent, but routing is needed to show different pages for URLs like /dashboard and /posts.
  • Routes are declared in an array and registered in app config with provideRouter(routes).
  • A route object binds a path to a component, and the path is a unique string without a leading slash.
  • router-outlet is required to render routed components inside the layout component.
  • routerLink replaces href for Angular navigation, and RouterLink must be imported for the links to work.
  • Route order matters because Angular checks routes from top to bottom and picks the first match.
  • Dynamic params use a colon in the path, such as pages/:pageId.
  • With withComponentInputBinding(), route params and query params can be passed into required signal inputs automatically.
  • redirectTo can point to a string or to a function that builds a target URL from route data.
  • The wildcard path ** handles undefined routes and must be placed last.
  • Nested routes use children, and the parent component needs its own router-outlet to render child routes.
  • routerLinkActive adds an active class, and exact matching avoids highlighting parent and child links at the same time.
  • Programmatic navigation uses Router, navigateByUrl for full URLs, and navigate for path arrays.
  • Lazy loading with loadComponent loads a component only when the route is visited.
  • A guard can block access by returning a boolean or observable value and redirecting if the user is not allowed.
  • A resolver can fetch or prepare data before route activation and expose it under a named property on the route.