The Route Interface
Routes are defined using theRoute 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
UseActivatedRoute to access route parameters:
user-detail.component.ts
Input Binding (Recommended)
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
Redirects
Static Redirects
Redirect from one path to another:routes.ts
Path Matching Strategies
ThepathMatch 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 usingRedirectCommand:
auth.guard.ts
Child Routes
Create nested route hierarchies with child routes:routes.ts
<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
