README
This is the coolest web component router
More documentation coming up later
Eagrouter is an easy to use component router for web components. It works just like any other web component. It takes in 2 properties, a route configuration object and an optional route base string. Eagrouter is really a wrapper around Pagejs which is a tiny express-inspired client-side router; if you like pagejs, then you would like Eagrouter. It uses almost all the same api's for declaring routes as pagejs . It also uses rxjs for reactivity and LitElement. This three libraries combined together makes the router awseome to use.
Installation
npm install eagrouter
Usage
import "eagrouter";
Use as a normal html tag in your project root, and provide your route config.
This example uses lit-element as a base class. But it works with other web component libraries also. Make sure all routes start with "/".
import { LitElement, html } from "lit";
export class AppRoot extends LitElement {
routes = [
{
path: "/",
redirect: "/home",
},
// For pages without lazyloading, use like this
{
component: "<home-page></home-page>",
path: "/home",
},
// For pages with lazyloading, use like this
{
component: "<products-page></products-page>",
path: "/products",
bundle: () => import("./pages/products"),
},
// Use * to match multiple paths
{
component: "<products-page></products-page>",
// Mathes all products path because of * symbol
path: "/products/*",
bundle: () => import("./pages/products"),
},
// If the component to be rendered has properties, add it to the props object like the example shown below.
{
component: "<products-page-two></products-page-two>",
path: "/products/pagetwo",
// Properties added to route here. The route property is a regular js object
props : {
propNameOne: 'some value'
propNameTwo: '30'
},
},
{
component: "<products-page-three></products-page-three>",
path: "/products/pagethree",
props : {
// passed a route property in this example
routes: [
{
component: "<products-page-custom></products-page-custom>",
path: "/products-custom",
bundle: () => import("./pages/custom"),
},
]
},
},
// use * for pages with routes that don't match.
{
path: "/*",
component: "<page-not-found></page-not-found>",
bundle: () => import("./pages/page-not-found"),
},
];
render() {
return html`<eag-router .routes=${this.routes}></eag-router>`;
}
}
customElements.define("app-root", AppRoot);
You can import these other things from eagrouter. More documentation coming soon.
import { Route, navigationEvents$, NavState, queryString$, param$, routerHistory } from "eagrouter";
// "queryString$, navigationEvents$ and param