This guide covers how routing works in an app built with Ionic and Angular.

The Angular Router is one of the most important libraries in an Angular application. Without it, apps would be single view/single context apps or would not be able to maintain their navigation state on browser reloads. With Angular Router, we can create rich apps that are linkable and have rich animations (when paired with Ionic of course). Let's look at the basics of the Angular Router and how we can configure it for Ionic apps.

A simple Route

For most apps, having some sort of route is often required. The most basic configuration looks a bit like this:

  1. import { RouterModule } from '@angular/router';
  2. @NgModule({
  3. imports: [
  4. ...
  5. RouterModule.forRoot([
  6. { path: '', component: LoginComponent },
  7. { path: 'detail', component: DetailComponent },
  8. ])
  9. ],
  10. })


The simplest breakdown for what we have here is a path/component lookup. When our app loads, the router kicks things off by reading the URL the user is trying to load. In our sample, our route looks for '', which is essentially our index route. So for this, we load the LoginComponent. Fairly straight forward. This pattern of matching paths with a component continues for every entry we have in the router config. But what if we wanted to load a different path on our initial load?

Handling Redirects

For this we can use router redirects. Redirects work the same way that a typical route object does, but just includes a few different keys.

  1. [
  2. { path: '', redirectTo: 'login', pathMatch: 'full' },
  3. { path: 'login', component: LoginComponent },
  4. { path: 'detail', component: DetailComponent }
  5. ];


In our redirect, we look for the index path of our app. Then if we load that, we redirect to the login route. The last key of pathMatch is required to tell the router how it should look up the path.

Since we use full, we're telling the router that we should compare the full path, even if ends up being something like /route1/route2/route3. Meaning that if we have:

  1. { path: '/route1/route2/route3', redirectTo: 'login', pathMatch: 'full' },
  2. { path: 'login', component: LoginComponent },


And load /route1/route2/route3 we'll redirect. But if we loaded /route1/route2/route4, we won't redirect, as the paths don't match fully.

Alternatively, if we used:

  1. { path: '/route1/route2', redirectTo: 'login', pathMatch: 'prefix' },
  2. { path: 'login', component: LoginComponent },


Then load both /route1/route2/route3 and /route1/route2/route4, we'll be redirected for both routes. This is because pathMatch: 'prefix' will match only part of the path.

Navigating to different routes

Talking about routes is good and all, but how does one actually navigate to said routes? For this, we can use the routerLink directive. Let's go back and take our simple router setup from earlier:

  1. RouterModule.forRoot([
  2. { path: '', component: LoginComponent },
  3. { path: 'detail', component: DetailComponent }
  4. ]);


Now from the LoginComponent, we can use the following HTML to navigate to the detail route.

  1. <ion-header>
  2. <ion-toolbar>
  3. <ion-title>Login</ion-title>
  4. </ion-toolbar>
  5. </ion-header>
  6. <ion-content padding>
  7. <ion-button [routerLink]="['/detail']">Go to detail</ion-button>
  8. </ion-content>


The important part here is the ion-button and routerLink directive. RouterLink works on a similar idea as typical hrefs, but instead of building out the URL as a string, it can be built as an array, which can provide more complicated paths.

We also can programmatically navigate in our app by using the router API.

  1. import { Component } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. @Component({
  4. ...
  5. })
  6. export class LoginComponent {
  7. constructor(private router: Router){}
  8. navigate(){
  9. this.router.navigate(['/detail'])
  10. }
  11. }


Both options provide the same navigation mechanism, just fitting different use cases.

A note on navigation with relative URLs: Currently, to support multiple navigation stacks, relative URLs are something not supported

Lazy loading routes

Now the current way our routes are setup makes it so they are included in the same chunk as the root app.module, which is not ideal. Instead, the router has a setup that allows the components to be isolated to their own chunks.

  1. import { RouterModule } from '@angular/router';
  2. @NgModule({
  3. imports: [
  4. ...
  5. RouterModule.forRoot([
  6. { path: '', redirectTo: 'login', pathMatch: 'full' },
  7. { path: 'login', loadChildren: './login/login.module#LoginModule' },
  8. { path: 'detail', loadChildren: './detail/detail.module#DetailModule' }
  9. ])
  10. ],
  11. })


While similar, the loadChildren property is a way to reference a module by string instead of a component directly. In order to do this though, we need to create a module for each of the components.

  1. ...
  2. import { RouterModule } from '@angular/router';
  3. import { LoginComponent } from './login.component';
  4. @NgModule({
  5. imports: [
  6. ...
  7. RouterModule.forChild([
  8. { path: '', component: LoginComponent },
  9. ])
  10. ],
  11. })

We're excluding some additional content and only including the necessary parts.

Here, we have a typical Angular Module setup, along with a RouterModule import, but we're now using forChild and declaring the component in that setup. With this setup, when we run our build, we will produce separate chunks for both the app component, the login component, and the detail component.

Working with Tabs

With Tabs, the Angular Router provides Ionic the mechanism to know what components should be loaded, but the heavy lifting is actually done by the tabs component. Let's look at a simple example.

  1. const routes: Routes = [
  2. {
  3. path: 'tabs',
  4. component: TabsPage,
  5. children: [
  6. {
  7. path: 'tab1',
  8. children: [
  9. {
  10. path: '',
  11. loadChildren: '../tab1/tab1.module#Tab1PageModule'
  12. }
  13. ]
  14. },
  15. {
  16. path: '',
  17. redirectTo: '/tabs/tab1',
  18. pathMatch: 'full'
  19. }
  20. ]
  21. },
  22. {
  23. path: '',
  24. redirectTo: '/tabs/tab1',
  25. pathMatch: 'full'
  26. }
  27. ];


Here we have a "tabs" path that we load. In this example we call the path “tabs”, but the name of the paths are open to be changed. They can be called whatever fits your app. In that route object, we can define a child route as well. In this example, the top level child route "tab1" acts as our "outlet", and can load additional child routes. For this example, we have a single sub-child-route, which just loads a new component. The markup for the tab is as followed:

  1. <ion-tabs>
  2. <ion-tab-bar slot="bottom">
  3. <ion-tab-button tab="tab1">
  4. <ion-icon name="flash"></ion-icon>
  5. <ion-label>Tab One</ion-label>
  6. </ion-tab-button>
  7. </ion-tab-bar>
  8. </ion-tabs>


If you've built apps with Ionic before, this should feel familiar. We create a ion-tabs component, and provide a ion-tab-bar. The ion-tab-bar provides a ion-tab-button with a tab property that is associated with the tab "outlet" in the router config. Note that the latest version of @ionic/angular no longer requires <ion-tab>, but instead allows developers to fully customize the tab bar, and the single source of truth lives within the router configuration.