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

# Routing

The router setup that lets Angular Router run on Lynx, deep-link routing, and the directive that marks the active link for screen readers. `import { provideRouter, provideDeepLinkRoute, type DeepLink, type DeepLinkRoute, LynxRouterLinkActive } from '@blotch/angular-lynx';`

The rest of the routing exports have pages of their own:

| Page                                                      | Exports                                                                                            |
| --------------------------------------------------------- | -------------------------------------------------------------------------------------------------- |
| [Stack Outlet](/api/angular-lynx/routing/stack-outlet.md) | `LynxStackOutlet`, `StackNavigationInfo`, `LynxStackPage`, `LynxStackChrome`, `LynxStackBottomBar` |
| [Tab Outlet](/api/angular-lynx/routing/tab-outlet.md)     | `LynxTabOutlet`, `LynxTabPage`                                                                     |
| [Location](/api/angular-lynx/routing/location.md)         | `LynxRouteReuseStrategy`, `LynxLocationStrategy`, `LynxPlatformLocation`                           |

## Usage

```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()),
  ],
};
```

- **Import `provideRouter` from `@blotch/angular-lynx`,** not `@angular/router`.
- **Everything else still comes from `@angular/router`:** `RouterOutlet`, `RouterLink`, `RouterLinkActive`, `Router`, `ActivatedRoute` and the rest.
- **`RouterLink` works on any Lynx element.** AngularLynx delivers a tap to its `click` listener.

## API

### `provideRouter()`

```typescript
function provideRouter(
  routes: Routes,
  ...features: RouterFeatures[]
): EnvironmentProviders;
```

Calls Angular's `provideRouter()` with your routes and features, and adds the Lynx pieces:

| Provides           | As                                                            | Why                                         |
| ------------------ | ------------------------------------------------------------- | ------------------------------------------- |
| `PlatformLocation` | `LynxPlatformLocation`                                        | Lynx has no URL bar and no browser history. |
| `LocationStrategy` | `LynxLocationStrategy`                                        | History lives in memory instead.            |
| Router config      | `withRouterConfig({ resolveNavigationPromiseOnError: true })` | A failed navigation can't crash the thread. |

- **Leaving a route destroys its components,** as on the web. `provideRouter()` keeps Angular's own `RouteReuseStrategy`. To keep route views alive instead, provide [`LynxRouteReuseStrategy`](/api/angular-lynx/routing/location.md#lynxroutereusestrategy).
- **Failed navigations resolve to `false`** instead of rejecting. Watch `Router.events` for `NavigationError` to detect them.
- **Your own `withRouterConfig()` replaces this one,** so include `resolveNavigationPromiseOnError: true` in it.
- **From an event handler,** call `router.navigate()` directly, as on the web.
- **It tells `LynxRouterLinkActive`** where to find an element's `RouterLinkActive`, so `LYNX_ELEMENTS` doesn't import the router itself. Without `provideRouter()`, active links don't get the `selected` trait.

#### Shortcuts

A [shortcut](/guide/shortcuts.md) with a `route` navigates there when the person runs it, from Siri or the app icon's menu. `provideRouter()` starts [`LynxShortcuts`](/api/angular-lynx/sharing/shortcuts.md) with the app, so it takes no code.

#### Deep links

A [deep link](/guide/deep-links.md) with one of angular.json's `deepLinkSchemes` navigates to its route: `myapp://orders/42?tab=items` goes to `/orders/42?tab=items`.

- **A link that starts the app** is its first route, with nothing before it in the history.
- **If the router can't go there,** such as when no route matches, the app starts at `/` and logs a warning.
- **A link while the app is open** navigates there from the current screen.
- [`provideDeepLinkRoute()`](#providedeeplinkroute) changes where links lead.

#### The Android back button

- **It calls `Location.back()`** while `LynxLocationStrategy.canGoBack()` is `true`.
- **It leaves the app** when `canGoBack()` is `false`, unless something open on top takes it first (see [`LynxBackButton`](/api/angular-lynx/host/back-button.md)).
- **It works in the Android app** `ng build --platform android` makes, and in [your own app](/guide/embedding/shell-module.md#connect-the-android-back-button) if it sends the same event.
- **With a `LocationStrategy` of your own,** the back button always leaves the app.

### `provideDeepLinkRoute()`

```typescript
function provideDeepLinkRoute(route: DeepLinkRoute): EnvironmentProviders;

type DeepLinkRoute = (link: DeepLink) => string | null;

type DeepLink = {
  readonly url: string;
  readonly route: string;
  readonly initial: boolean;
};
```

Decides where the router goes for a link that opens the app, as Expo Router's `redirectSystemPath` does.

```typescript
provideDeepLinkRoute(({ url, route }) =>
  url.startsWith('myapp://auth') ? null : route,
);
```

It's called with each link:

| Field     | What it is                                                         |
| --------- | ------------------------------------------------------------------ |
| `url`     | The whole link.                                                    |
| `route`   | The route the router would go to without `provideDeepLinkRoute()`. |
| `initial` | Whether the link started the app.                                  |

- **Return a route** to navigate to it.
- **Return `null`** to stay where the app is. For the link that started the app, that's `/`.
- **An error it throws** goes to the app's `ErrorHandler`, and the link goes nowhere.

`route` is the link's path with its query and fragment:

- **For the app's own scheme,** everything after `//` is the path, the host too. `myapp://orders/42` and `myapp:///orders/42` both lead to `/orders/42`.
- **For an `http` or `https` link,** the path starts after the host.

### `LynxRouterLinkActive`

```typescript
@Directive({ selector: '[routerLinkActive]' })
class LynxRouterLinkActive {}
```

Adds the `selected` [accessibility trait](/api/angular-lynx/accessibility.md#attributes) to an element while Angular's `RouterLinkActive` on it says its route is active. It takes it away after, so screen readers know which link is the current one.

- **It's part of `LYNX_ELEMENTS`,** so it's on every `routerLinkActive` element of a component that imports `LYNX_ELEMENTS` and `RouterLinkActive`.
- **It has no inputs or outputs** of its own.
- **It keeps the element's own traits.** `accessibility-traits="button"` reads `button,selected` while the link is active, and `button` again after. A trait set later is merged the same way.
- **Android leaves the element's traits alone.** It reads a single trait (`none`, `image` or `button`) and has no selected trait, so a list would only lose the element's own.
- **On a component's host,** add it to `hostDirectives` next to `RouterLinkActive`. Host directives don't match selectors, so `LYNX_ELEMENTS` can't add it there:

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

@Component({
  selector: 'app-tab',
  imports: [LYNX_ELEMENTS],
  hostDirectives: [
    { directive: RouterLink, inputs: ['routerLink: route'] },
    {
      directive: RouterLinkActive,
      inputs: ['routerLinkActive: activeClass'],
    },
    LynxRouterLinkActive,
  ],
  host: { 'accessibility-traits': 'button' },
  template: `<text><ng-content /></text>`,
})
export class Tab {
  readonly route = input.required<string>();
}
```

## Types

The routing pages use these Angular types:

- `Routes`, `RouterFeatures`, `ActivatedRouteSnapshot` and `DetachedRouteHandle` from `@angular/router`
- `LocationChangeListener` from `@angular/common`
- `Signal` from `@angular/core`

## Related

- [Routing and navigation](/guide/routing/index.md): routes, links, lazy loading and navigating from code.
- [Stack Outlet](/api/angular-lynx/routing/stack-outlet.md), [Tab Outlet](/api/angular-lynx/routing/tab-outlet.md) and [Location](/api/angular-lynx/routing/location.md)
- [Accessibility](/api/angular-lynx/accessibility.md#attributes): `accessibility-traits` and the other attributes.
- [Bootstrap](/api/angular-lynx/bootstrap.md)
- [Angular Router](https://angular.dev/guide/routing)
