@gluonjs/router / packages/router/src / NamedRouteDefinition
Interface: NamedRouteDefinition<Params>
Type Parameters
Params
Params extends RouteParamsRaw = RouteParamsRaw
Properties
params
readonlyparams:Params
Example
Declare the required parameter shape for a named route so typed navigation cannot omit its product ID:
import { html } from '@gluonjs/core';
import {
createMemoryHistory,
createRouter,
createRouterMatcher,
createRouterPlugin,
isRouteActive,
lazyRoute,
type LazyRouteComponent,
type NamedRouteDefinition,
type RouteComponentContext,
type RouteNamedMap,
} from '@gluonjs/router';
const productRoute: NamedRouteDefinition<{ id: string }> = { params: { id: '42' } };
const routeContract = { product: productRoute } satisfies RouteNamedMap;
const ProductPage = ({ route }: RouteComponentContext) =>
html`<h1>Product ${route.params.id}</h1>`;
const lazyProduct: LazyRouteComponent = lazyRoute(async () => ProductPage);
const router = createRouter({
history: createMemoryHistory(['/products/42']),
routes: [{ path: '/products/:id', name: 'product', component: lazyProduct }],
});
const matcher = createRouterMatcher([{ path: '/products/:id', name: 'product' }]);
const plugin = createRouterPlugin(router);
const current = router.resolve('/products/42');
const target = router.resolve('/products');
console.log(routeContract.product.params.id, matcher.resolvePath('/products/42'), isRouteActive(current, target), plugin);