addRoutes()
Registers route patterns with activities or factory functions.
addRoutes<
T extends {
[P in keyof T & string]:
| ActivityRouter.RouteArg<P>
| ActivityRouter.RouteArg<P>[];
},
>(...routes: T[]): this;
Parameters
- routes — One ore more route tables, mapping patterns to activities (or factory functions).
Description
This method registers routes on the application activity router. Each key in the table is a route pattern, and each value is an activity, factory function, or array of these. When the navigation path matches a pattern, the associated activities are activated. When the path no longer matches, they are deactivated. Activities that were (only) returned from a factory function are also unlinked after deactivation.
Dynamic route factory functions may also return undefined (void), in which case the route is treated as if nothing matched. This can be used to handle certain routes differently, e.g. by ‘redirecting’ to a different path using navigate().
- Pattern segments starting with
:are parameters (e.g.users/:userId). - A
*at the end matches any remaining path (e.g.files/*, or just*for a catch-all). The remaining path is captured as thepathparameter; for root matches on a bare*, this value is an empty string. - Routes are matched in registration order (first match wins).
- Routes are matched to the current navigation path asynchronously, as well as any time the navigation context or path changes.
Examples
app.addRoutes({
"users": new UsersListActivity(),
"users/:userId": [usersList, ({ userId }) => new UserDetailActivity(userId)],
"users/:userId/edit": ({ userId }) => canEdit(userId) ? new EditUserActivity(userId) : undefined,
"*": new NotFoundActivity(),
});
Related
class AppContext
A singleton class that represents the global application state.
