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();

Full configuration options →

How do I define routes?

Configuring routes →

How do I set up a viewport?

Viewports documentation →

How do I use hash-based routing instead of clean URLs?

Hash vs PushState routing →


Navigation methods →

How do I navigate programmatically?

Using the Router API →

Active CSS class →

How do I navigate to parent routes from nested components?

Ancestor navigation →

How do I pass query parameters?

Query parameters →

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.

Bypassing the router →


Route Parameters

How do I define route parameters?

Path and parameters →

How do I access route parameters in my component?

Lifecycle hooks →

How do I get all parameters including from parent routes?

Aggregate parameters → Route parameters guide →

How do I constrain parameters with regex?

Constrained parameters →


Route Protection

How do I protect routes (authentication)?

Router hooks →

How do I implement authorization (role-based access)?

Router hooks example →

How do I prevent navigation away from unsaved forms?

canUnload hook →

How do I redirect based on conditions?

Redirect from canLoad →


Lifecycle Hooks

How do I load data before showing a component?

loading hook →

How do I run code after a component is fully loaded?

loaded hook →

When do lifecycle hooks run?

Hook
When
Use For

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

Hook summary →

What's the difference between component hooks and router hooks?

  • Component hooks (IRouteViewModel): Implemented on the component itself

  • Router hooks (@lifecycleHooks()): Shared across multiple components

Router hooks vs component hooks →


Advanced Topics

How do I handle 404 / unknown routes?

Fallback configuration →

How do I create route aliases / redirects?

Redirects →

How do I work with multiple viewports (sibling routes)?

Sibling viewports →

How do I implement nested/child routes?

Hierarchical routing → Child routing playbook →

How do I lazy load routes?

Using inline import() →

How do I set/change the page title?

Setting titles → | Customizing titles →

How do I generate URLs without navigating?

Path generation →

How do I work with base paths (multi-tenant apps)?

Base path configuration →

How do I handle browser back/forward buttons?

History strategy →

How do I access the current route information?

Current route →


Troubleshooting

My routes don't work with clean URLs (no hash)

Problem: Getting 404 errors when refreshing or accessing routes directly

Solution:

  1. Ensure <base href="/"> is in your HTML

  2. Configure server for SPA routing (return index.html for all routes)

  3. Or use hash routing: useUrlFragmentHash: true

PushState configuration →

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:

  1. Check your link format - is it truly external?

  2. You probably don't need the external attribute anymore

  3. Links with protocol (https://, mailto:) are automatically bypassed

Only needed for edge cases:

Bypassing href →

Problem: Links to sibling routes not working

Solution: Use ../ prefix for parent context

Ancestor navigation →

My lifecycle hooks aren't being called

Problem: canLoad, loading, etc. not executing

Solution: Implement the IRouteViewModel interface

Lifecycle hooks →

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

Transition plans →

How do I debug routing issues?

Router events →


Complete Documentation

Last updated

Was this helpful?