Router API

The Dojo Router exposes an API that can be used to generate and navigate to links, get the params for the current route and check if a route id has been matched.

  • link(route: string, params: Params = {}): string | undefined: Generate a link based on the route id and optionally params. If no params are passed it will attempt to use the current routes parameters, then any default parameters provided in the routing configuration. If a link cannot be generated, undefined is returned.
  • setPath(path: string): void: Sets the path in the router.
  • get currentParams(): { [string: index]: string }: Returns parameters in the current route
  • getRoute(id: string): RouteContext | undefined: Returns the RouteContext for a route id if it is currently matched. If the route id is not matched, then return undefined.

Generating a link for a route

src/routes.ts

  1. export default [
  2. {
  3. id: 'home',
  4. path: 'home',
  5. outlet: 'home'
  6. },
  7. {
  8. id: 'about',
  9. path: 'about',
  10. outlet: 'about-overview',
  11. children: [
  12. {
  13. id: 'about-services',
  14. path: '{services}',
  15. outlet: 'about',
  16. defaultParams: {
  17. services: 'sewing'
  18. }
  19. },
  20. {
  21. id: 'about-company',
  22. path: 'company',
  23. outlet: 'about'
  24. },
  25. {
  26. id: 'about-history',
  27. path: 'history',
  28. outlet: 'about'
  29. }
  30. ]
  31. }
  32. ];

src/main.ts

  1. import Router from '@dojo/framework/routing/Router';
  2. import routes from './routes';
  3. const router = new Router(routes);
  4. // returns `#home`
  5. console.log(router.link('home'));
  6. // returns `#about`
  7. console.log(router.link('about-overview'));
  8. // returns `#about/company`
  9. console.log(router.link('about-company'));
  10. // returns `#about/history`
  11. console.log(router.link('about-history'));
  12. // returns `#about/knitting`
  13. console.log(router.link('about-services'), { services: 'knitting' });
  14. // Uses the current URL then default params to returns `#about/knitting`
  15. // when the current route is `#about/cooking` returns `#about/cooking`
  16. // when the current route does not contain the params returns `#about/sewing`
  17. console.log(router.link('about-services'));
  18. // returns `undefined` for an unknown route
  19. console.log(router.link('unknown'));

Changing a route

  1. import Router from '@dojo/framework/routing/Router';
  2. import routes from './routes';
  3. const router = new Router(routes);
  4. // goto `#home` route
  5. router.setPath('#home');

Getting the current parameters

  1. import Router from '@dojo/framework/routing/Router';
  2. import routes from './routes';
  3. const router = new Router(routes);
  4. // returns the current params for the route
  5. const params = router.currentParams;

Get a matched route

Use the getRoute to return the RouteContext for a matched route id, or undefined if the route id’s path is not matched.

RouteContext:

  • id: string: The route id
  • outlet: string: The outlet id
  • queryParams: { [index: string]: string }: The query params from the matched routing.
  • params: { [index: string]: string }: The path params from the matched routing.
  • isExact(): boolean: A function indicates if the route is an exact match for the path.
  • isError(): boolean: A function indicates if the route is an error match for the path.
  • type: 'index' | 'partial' | 'error': The type of match for the route, either index, partial or error.
  1. import Router from '@dojo/framework/routing/Router';
  2. import routes from './routes';
  3. const router = new Router(routes);
  4. // returns the route context if the `home` route is matched, otherwise `undefined`
  5. const routeContext = router.getRoute('home');