> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# Routing and navigation

Move between screens with Angular's Router, `<router-outlet>` and `routerLink`, as on the web.

What differs:

- **The URL lives in memory.** There's no address bar.
- **A link is any element you tap.**
- **The active link tells screen readers it's selected.**

For screens that slide in and out, or tabs that stay alive, see [Slide screens in and out](/guide/routing/stack.md) and [Keep each tab alive](/guide/routing/tabs.md).

## Set up the router

Import `provideRouter()` from `@blotch/angular-lynx` instead of `@angular/router`. `ng add @blotch/angular-lynx` sets this up for you.

```typescript title="src/app/app.config.ts"
import { type ApplicationConfig } from '@angular/core';
import { provideRenderer, provideRouter } from '@blotch/angular-lynx'; // [!code highlight]
import { routes } from './app.routes';

export const appConfig: ApplicationConfig = {
  providers: [
    provideRenderer(),
    provideRouter(routes), // [!code highlight]
  ],
};
```

```typescript title="src/app/app.routes.ts"
import type { Routes } from '@angular/router';
import { Home } from './home/home';
import { Settings } from './settings/settings';

export const routes: Routes = [
  { path: '', pathMatch: 'full', redirectTo: 'home' }, // [!code highlight]
  { path: 'home', component: Home },
  { path: 'settings', component: Settings },
];
```

- **Give the empty path `''` a route.** The app always starts at `/`, and there's no URL bar to start anywhere else. Without an `''` route, the first screen is blank.
- **`provideRouter()` takes the usual router features** as extra arguments, such as `withComponentInputBinding()`, `withRouterResources()` and [`withErrorBoundaries()`](/guide/error-handling.md#show-an-error-screen-for-a-route).

## Navigate between screens

```typescript title="src/app/app.ts"
import { Component } from '@angular/core';
import { RouterLink, RouterLinkActive, RouterOutlet } from '@angular/router';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

@Component({
  selector: 'app-root',
  imports: [LYNX_ELEMENTS, RouterOutlet, RouterLink, RouterLinkActive],
  template: `
    <view class="flex flex-row">
      <view class="p-3" routerLink="/home" routerLinkActive="bg-accent">
        <text>Home</text>
      </view>
      <view class="p-3" routerLink="/settings" routerLinkActive="bg-accent">
        <text>Settings</text>
      </view>
    </view>
    <router-outlet />
  `,
})
export class App {}
```

- **`routerLink` and `routerLinkActive` are Angular's own,** from `@angular/router`.
- **A tap on the element navigates.** `RouterLink` listens for `click`, and AngularLynx hands it the tap, as [Lynx for Web](/guide/glossary.md#web-preview) does.
- **Put `routerLink` on any element,** usually the `<view>` around a label.
- **An `<a routerLink>` works too.** It renders as a `<text>` (see [HTML tags](/guide/elements/index.md#html-tags)).
- **Its inputs work as on the web,** such as `queryParams`, `replaceUrl` and `state`. There's no `href` to show.
- **`routerLinkActive` adds its classes while the link's route is active.** For styling a class can't do, read its state: `#link="routerLinkActive"`, then `link.isActive`.

### The active link is selected for screen readers

While its route is active, `LYNX_ELEMENTS` adds `selected` to the element's `accessibility-traits`. It goes after any traits of the element's own, and comes off again after.

- **iOS** announces the element as selected.
- **Android** has no selected trait, so there the element keeps its own traits.

See [`LynxRouterLinkActive`](/api/angular-lynx/routing/index.md#lynxrouterlinkactive) for the details.

:::tip
For tabs along the bottom of the screen, the [Tab Bar](/dolan/tab-bar.md) component does all of this: `npx @blotch/dolan add tab-bar`. The [Dock](/dolan/dock.md) is the same as a floating Liquid Glass bar.
:::

### Navigate from code

```typescript title="src/app/order-row.ts"
import { Component, inject } from '@angular/core';
import { Router } from '@angular/router';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

@Component({
  selector: 'app-order-row',
  imports: [LYNX_ELEMENTS],
  template: `
    <view (bindtap)="open()">
      <text>Order #1042</text>
    </view>
  `,
})
export class OrderRow {
  readonly #router = inject(Router);

  open(): void {
    this.#router.navigate(['/orders', 1042]);
  }
}
```

- **Call `navigate()` or `navigateByUrl()` straight from a handler,** as on the web.
- **Failed navigations don't throw.** `navigate()` resolves to `false`, for example for a path with no route. Watch `Router.events` for `NavigationError` to find out why.

## Go back

Use `Location.back()` from `@angular/common`:

```typescript title="src/app/details/details.ts"
import { Location } from '@angular/common';
import { Component, inject } from '@angular/core';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

@Component({
  selector: 'app-details',
  imports: [LYNX_ELEMENTS],
  template: ` <view (bindtap)="back()"><text>Back</text></view> `,
})
export class Details {
  readonly #location = inject(Location);

  back(): void {
    this.#location.back(); // [!code highlight]
  }
}
```

- **`Location.back()` and `Location.forward()`** move through the router's in-memory history.
- **`history.back()` does nothing** on a device, because there's no browser history.
- **For a Back button in a top bar,** use the [Navigation Bar](/dolan/navigation-bar.md). It shows its Back button only while there's a screen to go back to, and calls `Location.back()`.

### The Android back button

In an app built with `ng build --platform android`, the Android back button goes back a screen.

- **The router handles it like the browser's back button** on the web, so guards such as `canDeactivate` still run.
- **On the first screen,** it leaves the app.
- **A popover or dialog open on top closes first.**
- **Your own code can take back** with [`LynxBackButton`](/api/angular-lynx/host/back-button.md).

## Read route parameters

Turn on `withComponentInputBinding()` to receive route parameters as inputs:

```typescript title="src/app/app.config.ts"
import { type ApplicationConfig } from '@angular/core';
import { withComponentInputBinding } from '@angular/router';
import { provideRenderer, provideRouter } from '@blotch/angular-lynx';
import { routes } from './app.routes';

export const appConfig: ApplicationConfig = {
  providers: [
    provideRenderer(),
    provideRouter(routes, withComponentInputBinding()), // [!code highlight]
  ],
};
```

```typescript title="src/app/profile/profile.ts"
import { Component, input } from '@angular/core';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

@Component({
  selector: 'app-profile',
  imports: [LYNX_ELEMENTS],
  template: `<text>Profile {{ id() }}</text>`,
})
export class Profile {
  readonly id = input.required<string>(); // from the `profile/:id` route
}
```

**Read parameters reactively,** through an input or `ActivatedRoute.paramMap`, rather than once in `ngOnInit`.

Angular reuses a component when only the params change, as on the web. `/profile/1` and `/profile/2` share one component instance, which updates in place.

## Lazy-loaded routes

`loadComponent` works:

```typescript title="src/app/app.routes.ts"
import type { Routes } from '@angular/router';

export const routes: Routes = [
  { path: '', pathMatch: 'full', redirectTo: 'home' },
  {
    path: 'home',
    loadComponent: () => import('./home/home').then((m) => m.Home),
  },
];
```

It doesn't split your code, though. The build keeps every route's code in the [bundle](/guide/glossary.md#bundle), so nothing downloads when the user navigates.

To make the bundle smaller, you can have the build load lazy routes as the app starts: see [`eagerImports`](/guide/building-for-production.md#make-the-script-smaller-with-eagerimports).

## Keep a screen alive

Leaving a route destroys its component, as on the web. `ngOnDestroy` runs, and its state is gone when you come back.

| To keep                        | Use                                                                                      | Until                 |
| ------------------------------ | ---------------------------------------------------------------------------------------- | --------------------- |
| The screens under the top one  | [`<lynx-stack-outlet>`](/guide/routing/stack.md)                                         | You go back past them |
| Each tab, native views and all | [`<lynx-tab-outlet>`](/guide/routing/tabs.md)                                            | You leave the outlet  |
| Every route view               | [`LynxRouteReuseStrategy`](/api/angular-lynx/routing/location.md#lynxroutereusestrategy) | The app closes        |

`LynxRouteReuseStrategy` keeps its views in memory, with their timers and subscriptions running. Their native views are built again each time.

## Differences from web Angular

| Feature      | Web Angular                                 | AngularLynx                            |
| ------------ | ------------------------------------------- | -------------------------------------- |
| URL          | Shown in the address bar                    | Kept in memory, never shown            |
| Starting URL | Any URL the browser opens                   | Always `/`                             |
| Links        | `<a routerLink>`                            | `routerLink` on any element, `<a>` too |
| Active link  | `aria-current` with `ariaCurrentWhenActive` | The `selected` accessibility trait     |
| Going back   | Browser back button, `history.back()`       | Android back button, `Location.back()` |
| Transitions  | `withViewTransitions()`                     | `<lynx-stack-outlet>`, with swipe back |
| Tabs         | Built again each time you switch to one     | `<lynx-tab-outlet>` keeps each one     |
| Lazy routes  | Separate files, downloaded on demand        | Part of the bundle                     |

## Related

- [Slide screens in and out](/guide/routing/stack.md): `<lynx-stack-outlet>`, with swipe back
- [Keep each tab alive](/guide/routing/tabs.md): `<lynx-tab-outlet>`
- [Routing API reference](/api/angular-lynx/routing/index.md)
- [Handling taps and events](/guide/elements/event-handling.md)
- [Multi-page apps](/guide/multi-page.md): separate bundles, each its own Angular app, instead of routes
- [Tab Bar](/dolan/tab-bar.md): a ready-made bottom tab bar that navigates between routes
- [Dock](/dolan/dock.md): the same as a floating Liquid Glass bar
- [Navigation Bar](/dolan/navigation-bar.md): a screen's top bar, with a Back button that goes back through the router
- [Angular routing guide](https://angular.dev/guide/routing)
