Route Events

The router emits several events via the Event Aggregator, allowing you to listen to router events. In some situations, you might opt for a router hook, but in other cases, an event might be what you are after.

A good example of where using events might be more appropriate is showing and hiding loaders and other parts of your applications related to routing.

The events fired are:

  • au:router:router-start

  • au:router:router-stop

  • au:router:navigation-start

  • au:router:navigation-end

  • au:router:navigation-cancel

  • au:router:navigation-error

To listen to these events, you subscribe to them using the event aggregator like this:

import { IEventAggregator } from 'aurelia';
import { IRouteableComponent } from '@aurelia/router'; 

export class MyComponent implements IRouteableComponent {    
    constructor(@IEventAggregator readonly ea: IEventAggregator) {

    }
    
    bound() {
        this.ea.subscribe('au:router:navigation-start', payload => {
            // Do stuff inside of this callback
        });
    }
 }   

As you might expect, these events are named in an intuitive way depending on the action taking place inside the router.

You will want to listen to the end, cancel and error navigation events if you're relying on displaying and hiding parts of the UI based on the router to ensure you're checking for a true "done" state.

Last updated

Was this helpful?

Revision created

refactor(event): no longer call prevent default by default (#1926) BREAKING CHANGE: no longer calling preventDefault on all events, this can be switched back to v1 behavior via IListenerBindingOptions [skip ci] * BREAKING CHANGE: no longer reexport fetch plugin from aurelia package * fix(router-lite): dont register config * chore: cleanup unused methods, correct typings on test fixture