Quick Reference ("How Do I...")
Navigate your Aurelia 2 application with confidence using this task-focused quick reference.
Table of Contents
Getting Started
How do I install and configure the router?
// Install
npm i @aurelia/router
// Configure in main.ts
import { RouterConfiguration } from '@aurelia/router';
Aurelia
.register(RouterConfiguration.customize({
useUrlFragmentHash: false, // Clean URLs (default)
historyStrategy: 'push', // Browser history
}))
.app(MyApp)
.start();How do I define routes?
How do I set up a viewport?
How do I use hash-based routing instead of clean URLs?
Navigation
How do I create navigation links?
How do I navigate programmatically?
How do I highlight the active link?
How do I navigate to parent routes from nested components?
How do I pass query parameters?
How do I handle external links?
Good news: External links work automatically! The router automatically ignores:
Only use external attribute for edge cases:
How it works: The router uses the URL constructor to check if a link is external. Any URL that can be parsed without a base (like https://, mailto:, etc.) is automatically treated as external.
Route Parameters
How do I define route parameters?
How do I access route parameters in my component?
How do I get all parameters including from parent routes?
Aggregate parameters → Route parameters guide →
How do I constrain parameters with regex?
Route Protection
How do I protect routes (authentication)?
How do I implement authorization (role-based access)?
How do I prevent navigation away from unsaved forms?
How do I redirect based on conditions?
Lifecycle Hooks
How do I load data before showing a component?
How do I run code after a component is fully loaded?
When do lifecycle hooks run?
canLoad
Before activation
Guards, redirects, param validation
loading
After approval, before render
Data fetching, state setup
loaded
After render
Analytics, scroll, post-render effects
canUnload
Before deactivation
Unsaved changes warnings
unloading
Before removal
Cleanup, save drafts
What's the difference between component hooks and router hooks?
Component hooks (
IRouteViewModel): Implemented on the component itselfRouter hooks (
@lifecycleHooks()): Shared across multiple components
Router hooks vs component hooks →
Advanced Topics
How do I handle 404 / unknown routes?
How do I create route aliases / redirects?
How do I work with multiple viewports (sibling routes)?
How do I implement nested/child routes?
Hierarchical routing → Child routing playbook →
How do I lazy load routes?
How do I set/change the page title?
Setting titles → | Customizing titles →
How do I generate URLs without navigating?
How do I work with base paths (multi-tenant apps)?
How do I handle browser back/forward buttons?
How do I access the current route information?
Troubleshooting
My routes don't work with clean URLs (no hash)
Problem: Getting 404 errors when refreshing or accessing routes directly
Solution:
Ensure
<base href="/">is in your HTMLConfigure server for SPA routing (return index.html for all routes)
Or use hash routing:
useUrlFragmentHash: true
External links are triggering the router (rare)
Problem: External links somehow being handled by router
This should NOT happen - the router automatically ignores external links like https://, mailto:, tel:, etc.
If it's happening:
Check your link format - is it truly external?
You probably don't need the
externalattribute anymoreLinks with protocol (
https://,mailto:) are automatically bypassed
Only needed for edge cases:
Navigation isn't working from nested components
Problem: Links to sibling routes not working
Solution: Use ../ prefix for parent context
My lifecycle hooks aren't being called
Problem: canLoad, loading, etc. not executing
Solution: Implement the IRouteViewModel interface
Route parameters aren't updating when navigating between same routes
Problem: Navigating from /users/1 to /users/2 doesn't update component
Solution: Configure transition plan
How do I debug routing issues?
Complete Documentation
Last updated
Was this helpful?