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

# Keep each tab alive

Switch tabs without rebuilding them, as a native tab bar does. Put `<lynx-tab-outlet>` where `<router-outlet>` was:

```typescript title="src/app/app.ts"
import { Component } from '@angular/core';
import { LYNX_ELEMENTS, LynxTabOutlet } from '@blotch/angular-lynx'; // [!code highlight]
import { UiDock, UiDockItem } from './components/ui/dock';

@Component({
  selector: 'app-root',
  imports: [LYNX_ELEMENTS, LynxTabOutlet, UiDock, UiDockItem], // [!code highlight]
  template: `
    <view class="flex h-full flex-col bg-background">
      <lynx-tab-outlet />
      <!-- [!code highlight] -->
      <ui-dock>
        <ui-dock-item route="/home" icon="home" label="Home" />
        <ui-dock-item route="/library" icon="music" label="Library" />
      </ui-dock>
    </view>
  `,
})
export class App {}
```

## Why

With `<router-outlet>` beside a [Tab Bar](/dolan/tab-bar.md) or [Dock](/dolan/dock.md), switching tabs destroys the tab you leave. The next one is built from scratch: its components, its elements, and its native views.

So a `<canvas>` sets up its WebGL context again, and a `<webview>` loads its page again. A native tab bar keeps each tab once you've seen it, and `<lynx-tab-outlet>` does the same.

## How it works

- **Each route under it is a tab.**
- **It's a router outlet,** with `<router-outlet>`'s inputs and outputs. It works with child routes, guards, resolvers, lazy routes, `withComponentInputBinding()`, route `resources` and [route error boundaries](#when-a-tab-fails).
- **The tab bar and the dock drive it** as they drive `<router-outlet>`.
- **The first visit builds a tab, and switching away keeps it.**
- **The tab you leave is hidden:** out of sight, out of screen readers' reach, and out of the way of taps.
- **Switching back shows it as you left it,** without building anything. It keeps its scroll positions, its native views, and its `<lynx-stack-outlet>` with the screens pushed on it.

### A tab's item goes back to where you left the tab

A navigation to a hidden tab's own URL (`/library`, what its item links to) goes to the URL the tab was at (`/library/album/3`).

- **From the tab itself,** the same link goes to `/library`. That pops its stack to its first screen, as tapping the selected tab does on iOS.
- **Any other URL goes where it says:** a deeper one (`/library/album/7`), or one with a query.
- **A navigation whose `info` is `{ stack: 'root' }`** goes where it says too, and starts the tab's stack again at that URL.

### Back goes through a tab's screens first

When a tab comes back with a stack, its screens' history entries come back before the current one. So Back, Android's back button and a swipe go to the screen under the top one, then on to the tab you came from.

### Pause a hidden tab

A hidden tab's components keep running. Inject [`LynxTabPage`](/api/angular-lynx/routing/tab-outlet.md#lynxtabpage) and read its `active()` signal to pause what only matters while the tab is shown, such as a 3D scene's frames or a poll.

The screens of a stack in a hidden tab say so too: their `LynxStackPage.active()` is `false`.

## Lazy-load a tab's routes

A route with only `loadChildren` is a tab under its own path, when its loaded routes start with the tab's component on `''`:

```typescript title="src/app/app.routes.ts"
export const routes: Routes = [
  { path: 'library', loadChildren: () => import('./library/routes') },
];
```

```typescript title="src/app/library/routes.ts"
export default [
  { path: '', component: LibraryStack, children: [/* the tab's screens */] },
] satisfies Routes;
```

`keep`, `prebuild`, and its route's `keepAlive` and `prebuild` data name it `library`, as its tab bar item does.

## Choose which tabs to keep

**Every tab you've shown is kept,** bounded by the number of tabs. To let one go when you leave it:

- **Give its route `data: { keepAlive: false }`.**
- **Or list the paths of the tabs to keep:** `<lynx-tab-outlet [keep]="['home', 'library']" />`.

Leaving the outlet's route, as signing out does, destroys every tab in it.

## Build tabs ahead

Build tabs before they're first shown, so the first tap on each is as quick as coming back to it:

```html
<lynx-tab-outlet [prebuild]="['library', 'profile']" />
```

Or put `data: { prebuild: true }` on a tab's route.

- **When:** once the first tab is drawn, the outlet builds them one at a time, each between frames.
- **Hidden:** it hides them as it hides a tab you've left.
- **What each gets:** its component and the screens its URL leads to (its stack's first screen), with their guards and resolvers run. Its `LynxTabPage.active()` is `false`.
- **Nothing else moves:** the URL, the history and the tab bar's selection stay put.
- **Tapping a tab while it's being built** builds it once.

### Which tabs can be built ahead

The outlet builds the tabs of the routes beside the one shown that have:

- a path without a parameter, so not `item/:id`
- a component, or children that start with the tab's component on `''`

It doesn't build a tab it doesn't keep. In development, it warns in the console of a tab it's asked for and can't build.

## When a tab fails

With Angular's [route error boundaries](/guide/error-handling.md#show-an-error-screen-for-a-route), a tab that throws shows its route's error component in its place:

```typescript title="src/app/app.config.ts"
provideRouter(
  routes,
  withErrorBoundaries({ defaultErrorComponent: ScreenError }),
);
```

- **A tab showing its error component isn't kept.** Switching back to it builds it again, as `<router-outlet>` does.
- **A hidden tab keeps running, so it can fail too.** Its error component waits there, and the tab is built again when you switch back to it. That error component gets only `error`, even with `withComponentInputBinding()`: `<router-outlet>` binds the route's params and data to its one screen, and the router is on the tab shown. Make the error component's route inputs optional.
- **A tab that fails as it's [built ahead](#build-tabs-ahead)** is left out, and its error goes to the app's `ErrorHandler`, not to the boundary's error component or `onError`. Angular has nothing that builds a route before you go to it: its boundary acts for the route the router is on, and a `RedirectCommand` from a tab you haven't opened would take the app somewhere you didn't ask to go. Once it's built, the boundary catches its errors as it does a tab you've shown. To see the error component, open the tab rather than building it ahead.

## Good to know

- **A plain `<router-outlet>` in a tab builds its screen again** when the tab comes back, as it does everywhere: the router leaves its route as the tab hides. Use `<lynx-stack-outlet>` for a tab's screens, or another `<lynx-tab-outlet>` for tabs inside a tab, to keep them with it.
- **The tab outlet adds a guard to each tab's route,** to take a tab bar back where the tab was left. It's on the router's copy of the route, so your `Routes` stay as you wrote them. The guard does show in `router.config`.
- **A stack inside a screen of another stack is kept too,** while a screen pushed over it covers it.

### Tabs built ahead

- **Nothing in a tab built ahead has appeared** until the tab is shown. It's off screen, so `binduiappear` waits for the first tap.
- **Something an app makes only for what's on screen** is still made then, such as a `<webview>` made on `binduiappear`. To have it ready, make it for what the tab shows first, without waiting.
- **The routes above a tab built ahead run their guards and resolvers again,** as a navigation to the tab from elsewhere in the app does.
- **The tab's own guards and resolvers run again when you tap it,** as they do for any tab you come back to.

## Related

- [`LynxTabOutlet` API](/api/angular-lynx/routing/tab-outlet.md)
- [Slide screens in and out](/guide/routing/stack.md): a stack of screens in each tab
- [Routing and navigation](/guide/routing/index.md): set up, links, going back
- [Tab Bar](/dolan/tab-bar.md) and [Dock](/dolan/dock.md): the bars that drive the tab outlet
