Skip to main content
Routes define the mapping between URL paths and the components that should be displayed. This guide covers all aspects of route configuration.

The Route Interface

Routes are defined using the Route interface from @angular/router:
The complete Route interface is defined in packages/router/src/models.ts:567 with extensive documentation for each property.

Basic Route Configuration

Simple Routes

The most basic route maps a path to a component:
app.routes.ts

Route Parameters

Define dynamic route segments using the :paramName syntax:
routes.ts

Accessing Route Parameters

Use ActivatedRoute to access route parameters:
user-detail.component.ts
With component input binding enabled, route parameters automatically bind to component inputs:
main.ts
user-detail.component.ts

Wildcard Routes

Handle unknown routes with a wildcard route:
routes.ts
Always place the wildcard route ** last in your route configuration. Routes are evaluated in order, and the wildcard matches everything.

Redirects

Static Redirects

Redirect from one path to another:
routes.ts

Path Matching Strategies

The pathMatch property determines how the router matches URLs:
  • 'prefix' (default): Matches if the URL starts with the path
  • 'full': Matches only if the URL exactly equals the path

Dynamic Redirects with RedirectCommand

Create conditional redirects using RedirectCommand:
auth.guard.ts

Child Routes

Create nested route hierarchies with child routes:
routes.ts
The parent component needs a <router-outlet> for child routes:
dashboard.component.ts

Componentless Routes

Group routes without specifying a component:
routes.ts

Route Data and Title

Static Data

Pass static data to routes:
routes.ts
profile.component.ts

Page Titles

Set page titles for better SEO and user experience:
routes.ts
user-title.resolver.ts

Custom URL Matchers

For complex URL patterns, use custom matchers:
file-matcher.ts
routes.ts
URL matchers provide flexibility for routes that can’t be expressed with standard path syntax. See packages/router/src/models.ts:181 for the UrlMatcher type definition.

Route Providers

Provide services scoped to a route and its children:
routes.ts

Complete Example

Here’s a comprehensive route configuration:
app.routes.ts

Best Practices

Order Matters

Define routes from most specific to least specific. The router uses first-match-wins strategy.

Use pathMatch: 'full'

Always use pathMatch: 'full' with redirects from empty path to avoid infinite loops.

Type-Safe Parameters

Use paramMap.get() instead of direct params access for better type safety.

Centralize Route Data

Use the data property to store route metadata like titles, breadcrumbs, and permissions.

Next Steps

Route Guards

Learn how to protect routes and control navigation with guards

Lazy Loading

Optimize your app by lazy loading feature modules