Router Tutorial
Introduction
Aurelia comes with a powerful fully-featured router without the need to install any additional dependencies. If you are new to Aurelia, we recommend visiting the Getting Started section first to familiarise you with the framework.
You are here because you want to familiarize yourself with the router, so we'll make this quick. At the end of this tutorial, you will be familiar with all the concepts and APIs you need to add routing into your Aurelia applications.
While building our recipe application, we will cover the following:
How to create and configure routes
Navigating with
load
in your views as well as view-modelsStyling active links
Programmatically navigating using router APIs
Working with route parameters
A working demo and code for the following tutorial can also be found here.
Installation
To do this tutorial, you'll need a working Aurelia application. We highly recommend following the Quick Start guide to scaffold an application. However, for this tutorial, we have a starter Aurelia 2 application ready to go that we recommend. It will allow you to follow along and live code.
As you will see, it's a boring application with no routing or anything exciting. All it says is Recipes (hardly a recipe application).
Add routes and a viewport
The viewport is where our loaded routes are dynamically loaded into. When we click on a route, the <au-viewport>
element will be populated with the requested component.
Open my-app.html
and add in the <au-viewport>
and some navigation links.
This won't do anything just yet because we haven't enabled routing or created any routes.
Enable routing
Let's go into main.ts
and configure Aurelia to use routing. Import the RouterConfiguration
object and pass it to Aurelia's register
method.
We also configure the router to use push-state routing instead of the hash-based router. This gives us cleaner URLs.
Create some routes
Now that we have a viewport and the router enabled let's create some routes. We will start by adding our routes to our root my-app.ts
component.
The important thing to note here is that we are not specifying a component to load for these routes. We will do that shortly, but the routing structure is what we are creating here.
The first route has an empty path
, which means it's a default route (the router will load this first if no other route is requested). The second route recipes
tells the router when the user visits /recipes
to load our recipes component. Lastly, the recipes/:recipeId
route has a route parameter that allows us to load specific recipes.
Create some components
We now need to create three components for our routes: the homepage, recipes page, and recipe detail page.
Unlike other frameworks and libraries, Aurelia works on the premise of a view-and-view model. If you familiarize yourself with the Getting Started section, you would already be familiar with these concepts.
Let's create the homepage first:
Let's create the recipes page:
Let's create the recipe detail page:
Lastly, let's import our components in my-app.ts
for the routes to load them.
Listing the recipes
In a non-tutorial application, you would have your API and server providing this data. But, for our tutorial will use a public recipe API instead. We could use mock data, but it would be a lot of data for a recipe application.
The MealDB is a fantastic free meal API that can give us recipes. We will use the Aurelia Fetch Client to get this data, which wraps up the native Fetch API.
In recipes-page.ts
add the following to the component view model:
We've loaded the recipes. Now it's time to display them. Open recipes-page.html
and add in the following:
We use Aurelia's repeat.for
functionality to loop over the recipes. But take notice of the link with load
attribute. Aurelia Router sees this and knows this is a route. We are providing the recipe detail route with the ID of the recipe.
It's not pretty, but we now have a list of recipes.
Add in support for 404 fallback
Sometimes a user might attempt to visit a recipe or page that doesn't exist. You might want to redirect the user or display a 404 page in those situations.
Let's create another component and call it fourohfour-page
We then specify on our <au-viewport>
what our fallback is. We need to import this 404 component to use it.
Reading route parameters
When we created our routes in my-app.ts
you might recall we created a recipe detail route which had a recipeId
parameter. Now, we are going to modify our recipe-detail
component to read this value from the URL and load the content.
There is a little more to unpack here. We inject the router because we will programmatically redirect away if the user attempts to view a non-existent recipe ID. We use the canLoad
method because loading our recipe view relies on existing recipes. If the recipe can't be found using the API, we redirect to the recipes page programmatically using the router.load
method.
Inside recipe-detail.html
we'll render our recipe:
The API we use returns ingredients on a line-by-line basis, so we've omitted those from this example. Now, you should be able to run your application and click on recipes to view them. A headline, image and instructions should now be visible.
As an additional step, you could add those in yourself.
Styling active links
The router automatically adds a active
class to all route links when they are active. Because our routes all live in my-app
We will edit my-app.css
We don't even have to import it (Aurelia does that automatically for us).
The active
class is now bold when active. We also do some quick styling tweaks to the route links to remove the underline and make them black so we can see the bold stand out more.
That's it
You just built a recipe application. It doesn't allow you to create recipes, and it's not very pretty, but it works. To see the application in action, a working demo of the above code can be seen here (or below).
Last updated