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 and Keep each tab alive.
Set up the router
Import provideRouter() from @blotch/angular-lynx instead of @angular/router. ng add @blotch/angular-lynx sets this up for you.
- 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 aswithComponentInputBinding(),withRouterResources()andwithErrorBoundaries().
Navigate between screens
routerLinkandrouterLinkActiveare Angular's own, from@angular/router.- A tap on the element navigates.
RouterLinklistens forclick, and AngularLynx hands it the tap, as Lynx for Web does. - Put
routerLinkon any element, usually the<view>around a label. - An
<a routerLink>works too. It renders as a<text>(see HTML tags). - Its inputs work as on the web, such as
queryParams,replaceUrlandstate. There's nohrefto show. routerLinkActiveadds its classes while the link's route is active. For styling a class can't do, read its state:#link="routerLinkActive", thenlink.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 for the details.
Navigate from code
- Call
navigate()ornavigateByUrl()straight from a handler, as on the web. - Failed navigations don't throw.
navigate()resolves tofalse, for example for a path with no route. WatchRouter.eventsforNavigationErrorto find out why.
Go back
Use Location.back() from @angular/common:
Location.back()andLocation.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. 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
canDeactivatestill 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.
Read route parameters
Turn on withComponentInputBinding() to receive route parameters as inputs:
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:
It doesn't split your code, though. The build keeps every route's code in the 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.
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.
LynxRouteReuseStrategy keeps its views in memory, with their timers and subscriptions running. Their native views are built again each time.
Differences from web Angular
Related
- Slide screens in and out:
<lynx-stack-outlet>, with swipe back - Keep each tab alive:
<lynx-tab-outlet> - Routing API reference
- Handling taps and events
- Multi-page apps: separate bundles, each its own Angular app, instead of routes
- Tab Bar: a ready-made bottom tab bar that navigates between routes
- Dock: the same as a floating Liquid Glass bar
- Navigation Bar: a screen's top bar, with a Back button that goes back through the router
- Angular routing guide
