createRouterModule() function
Home > @rimitive/router > createRouterModule
createRouterModule() function
Section titled “createRouterModule() function”Create a Router module for use with compose().
Like createElModule(adapter), this takes configuration at module creation time and returns a Module that can be composed with other modules.
Signature:
createRouterModule: (routes: RouteConfig[], options?: RouterOptions) => Module<"router", Router, { signal: SignalFactory; computed: ComputedFactory;}>Parameters
Section titled “Parameters”|
Parameter |
Type |
Description |
|---|---|---|
|
routes |
Route configuration (pure data) | |
|
options |
(Optional) Router options (initialPath, etc.) |
Returns:
Module<“router”, Router, { signal: SignalFactory; computed: ComputedFactory; }>
Example
Section titled “Example”import { compose } from '@rimitive/core';import { SignalModule, ComputedModule } from '@rimitive/signals/extend';import { createRouterModule } from '@rimitive/router';
const routes = [ { id: 'home', path: '' }, { id: 'about', path: 'about' }, { id: 'products', path: 'products', children: [ { id: 'product-detail', path: ':id' } ]}];
const svc = compose( SignalModule, ComputedModule, createRouterModule(routes, { initialPath: '/' }));
// Router is now part of the composed contextconst { router, signal, computed } = svc;
// Use in view layermatch(router.matches, (matches) => { const route = matches[0]; if (!route) return NotFound(); return componentMap[route.id]({ params: route.params });});