Documentation

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

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

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