For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /api/angular-lynx/routing/index.md.

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:

PageExports
Stack OutletLynxStackOutlet, StackNavigationInfo, LynxStackPage, LynxStackChrome, LynxStackBottomBar
Tab OutletLynxTabOutlet, LynxTabPage
LocationLynxRouteReuseStrategy, LynxLocationStrategy, LynxPlatformLocation

Usage

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()

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

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

ProvidesAsWhy
PlatformLocationLynxPlatformLocationLynx has no URL bar and no browser history.
LocationStrategyLynxLocationStrategyHistory lives in memory instead.
Router configwithRouterConfig({ 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.
  • 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 with a route navigates there when the person runs it, from Siri or the app icon's menu. provideRouter() starts LynxShortcuts with the app, so it takes no code.

A deep link 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() 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).
  • It works in the Android app ng build --platform android makes, and in your own app if it sends the same event.
  • With a LocationStrategy of your own, the back button always leaves the app.

provideDeepLinkRoute()

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.

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

It's called with each link:

FieldWhat it is
urlThe whole link.
routeThe route the router would go to without provideDeepLinkRoute().
initialWhether 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

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

Adds the selected accessibility trait 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:
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