@joist/router
Version:
Simple JS router
83 lines (62 loc) • 1.97 kB
Markdown
# /router
A simple, fairly naive router that can render and views it is given, whether they are JoistElements or not.
#### Installation:
```BASH
npm i /di /router /component
```
#### Example:
```TS
import { component, get, JoistElement } from '@joist/component';
import { template, html } from '@joist/component/lit-html';
import { Route, RouteCtx } from '@joist/router';
import { Child2Element } from './child-2.element';
const routes: Route[] = [
// Eager component route
{ path: '/foo-1', component: () => document.createElement('child-1') },
// Eager component route from a CustomElementConstructor
{ path: '/foo-2', component: () => Child1Element },
// Lazy component route
{ path: '/bar-1', component: () => import('child-2.element').then(() => document.createElement('child-2')) },
// Lazy component route
{ path: '/bar-2', component: () => import('child-2.element').then(m => m.Child2Element) },
// Child Paths. Will Match on any router that starts with /child-1
{ path: '/child-1(.*)', component: () => Child1Element }
];
export class AppElement extends JoistElement {}
class Child1Element extends JoistElement {
private route: RouteCtx;
connectedCallback() {
super.connectedCallback();
console.log(this.route.value);
this.route.onChange(ctx => {
console.log(ctx);
})
}
}
```