Routing
Router providers and the in-memory location classes that let Angular Router run on Lynx. import { provideRouter, LynxLocationStrategy, LynxPlatformLocation, LynxRouteReuseStrategy } from '@blotch/angular-lynx';
Usage
Import provideRouter from @blotch/angular-lynx, not @angular/router. Everything else, such as RouterOutlet, RouterLink, Router and ActivatedRoute, still comes from @angular/router.
API
provideRouter()
Calls Angular's provideRouter() with your routes and features, and adds the Lynx pieces:
- Failed navigations resolve to
falseinstead of rejecting. WatchRouter.eventsforNavigationErrorto detect them. - Your own
withRouterConfig()replaces this one, so includeresolveNavigationPromiseOnError: truein it. - From an event handler, wrap
router.navigate()insetTimeout. See thesetTimeoutrule.
LynxRouteReuseStrategy
Keeps each route's component alive when you navigate away, and reattaches it when you come back. Lynx holds only a limited number of native elements (its element pool), so recreating route views on every navigation crashes the app after a few round trips.
ngOnDestroydoesn't run when you leave a route, and component state survives.- Views are stored by route path, so
/user/1and/user/2share one stored view.
LynxRouteReuseStrategy.shouldDetach()
Always returns true.
LynxRouteReuseStrategy.store()
Stores the view under the route's path, or forgets it when handle is null.
LynxRouteReuseStrategy.shouldAttach()
Returns true when a view is stored for the route's path.
LynxRouteReuseStrategy.retrieve()
Returns the stored view, or null.
LynxRouteReuseStrategy.shouldReuseRoute()
Returns true when both snapshots come from the same route config, as Angular's default does.
LynxLocationStrategy
A LocationStrategy that keeps a history stack in memory, starting at /. Angular's Location service calls it, so you rarely call it yourself.
LynxLocationStrategy.path()
Returns the current URL, including its query string.
LynxLocationStrategy.prepareExternalUrl()
Returns internal unchanged.
LynxLocationStrategy.getBaseHref()
Always returns '/'.
LynxLocationStrategy.getState()
Returns the current entry's state, or null.
LynxLocationStrategy.pushState()
Adds a history entry and drops any forward entries.
LynxLocationStrategy.replaceState()
Replaces the current history entry.
LynxLocationStrategy.back() and forward()
Same as historyGo(-1) and historyGo(1).
LynxLocationStrategy.historyGo()
Moves through history and notifies popstate listeners. A move past either end does nothing.
LynxLocationStrategy.onPopState()
Adds a popstate listener. Call the returned function to remove it.
LynxPlatformLocation
A PlatformLocation with its own in-memory history. It parses URLs by hand, because the Lynx JavaScript environment has no URL class.
LynxPlatformLocation.href
The current URL as lynx://app followed by its path, query string and fragment.
LynxPlatformLocation.protocol, hostname and port
Fixed values, because there's no real server.
LynxPlatformLocation.pathname, search and hash
Parts of the current URL. pathname is '/' when empty. search keeps its ? and hash keeps its #, or each is ''.
LynxPlatformLocation.getBaseHrefFromDOM()
Always returns '/'.
LynxPlatformLocation.getState()
Returns the current entry's state.
LynxPlatformLocation.pushState() and replaceState()
Adds an entry, dropping forward entries, or replaces the current one.
LynxPlatformLocation.back(), forward() and historyGo()
Move through history and notify popstate listeners. A move past either end does nothing.
LynxPlatformLocation.onPopState() and onHashChange()
onPopState adds a listener and returns its remover. onHashChange does nothing, because hash changes arrive as popstate.
Types
This page uses only Angular types: Routes, RouterFeatures, ActivatedRouteSnapshot and DetachedRouteHandle from @angular/router, and LocationChangeListener from @angular/common.
Related
- Routing and navigation: routes, lazy loading and navigating from a tap.
- Threads: why navigation from a handler needs
setTimeout. - Bootstrap
- Angular Router
