Getting started
Routing with Aurelia feels like a natural part of the framework. It can easily be implemented into your applications in a way that feels familiar if you have worked with other frameworks and library routers. Here is a basic example of routing in an Aurelia application using router-lite
.
The following getting started guide assumes you have an Aurelia application already created. If not, consult our Quick Start to get Aurelia installed in minutes.
Installation
Configure the router-lite
router-lite
To use the router-lite
, we have to register it with Aurelia. We do this at the bootstrapping phase.
Create the routable components
For this example, we have a root component which is MyApp
. And then we are going to define two routes for the root component, namely home
and about
.
Let us define these components first.
Configure the routes
With the routable components in place, let's define the routes. To this end, we need to add the route configurations to our root component MyApp
.
There are couple of stuffs to note here. We start by looking at the configurations defined using the @route
decorator where we list out the routes under the routes
property in the configuration object in the @route
decorator. The most important things in every route configurations are the path
and the component
properties. This instructs the router to use the defined component
in the viewport when it sees the associated path
.
The viewport is specified in the view (see my-app.html
) by using the <au-viewport>
custom element. For example, the router will use this element to display the Home
component when it sees the /
(the empty path) or the /home
paths.
The nav>a
elements are added to navigate from one view to another.
See this in action:
Using pushstate
Live examples
For the documentation of router-lite, many live examples are prepared. It is recommended to use the live examples as you read along the documentation. However, if you are feeling adventurous enough to explore the features by yourself, here is the complete collection of the live examples at your disposal.
Last updated
Was this helpful?