Declaring Routes
You can add routes individually or organize them into a route group when parts of your app share a common URL prefix.
See the dedicated documentation about route groups.
Add Individual Routes
Routes support dynamic parameters (e.g. :name), which are passed as attributes to the corresponding component.
/// <reference types="@jadis/core/jsx-runtime" />
/** @jsxImportSource @jadis/core */
import { Jadis, createSelector, Router, defineRoutes } from '@jadis/core';
class HelloPage extends Jadis {
static selector = createSelector('hello-page');
_refs = this.useRefs((ref) => ({
name: ref('#name'),
}));
templateHtml() {
return <h1>Hello, <span id="name"></span>!</h1>;
}
onConnect() {
const name = this.getAttribute('name');
this._refs.name.textContent = name || 'Guest';
}
}
HelloPage.register();
const routes = defineRoutes({
hello: { path: '/hello/:name', page: HelloPage },
});
const myRouter = new Router(routes);
myRouter.mountOn(document.getElementById('app'));Navigating to /hello/john, the router will render:
<body>
<div id="app">
<hello-page name="john"></hello-page>
</div>
</body>Navigation
You can navigate to any defined route by using the name used as the key. If the route has parameters, pass them as the second argument of the goto method: TypeScript requires each of them by name, and refuses a second argument for a route without parameters.
const routes = defineRoutes({
inv: { path: '/invoice/:id', page: InvoicePage },
});
const myRouter = new Router(routes);
myRouter.mountOn(document.getElementById('app'));
myRouter.goto('inv', { id: 'abcd' });This will navigate /invoice/abcd
An address only known at run time, carried by a link or a menu, goes through gotoPath, its query included; match gives the route of a path without going there, or undefined:
myRouter.gotoPath('/invoice/abcd?tab=lines');
myRouter.match('/invoice/abcd'); // { name: 'inv', params: { id: 'abcd' }, … }replace(name, params) and replacePath(path) navigate in place of the current history entry, so back skips the page left: for a page that only sends on to another.
Use a Root Component
You can specify a root component (for example, a layout or wrapper) into which the route component will be injected. This is useful for shared layouts, navigation bars, or page wrappers.
const routes = defineRoutes({
hello: {
path: '/hello/:name',
page: HelloPage,
options: { rootComponentSelector: LayoutComponent.selector }
},
})
const myRouter = new Router(routes);
myRouter.mountOn(document.getElementById('app'));Navigating to /hello/john, the router will render:
<body>
<div id="app">
<layout-component name="john">
<hello-page name="john"></hello-page>
</layout-component>
</div>
</body>