1.1.3 • Published 2 years ago
vanjs-routing v1.1.3
VanJS Routing
The cleanest, simplest declarative routing solution for the VanJS framework. If you are coming
from React, vanjs-routing feels similar to react-router.
Install
yarn add vanjs-routing vanjs-corenpm i -S vanjs-routing vanjs-coreFeatures
- Declare routes with
Router()using a clean, simple and concise syntax - Use
Link()instead ofa()to navigate between pages - Use
navigate()in areasLinkcannot be used. (E.g. In a side-effect) - Access the router internal state
- Get the current pathname with
getRouterPathname() - Get the dynamic URL parameters with
getRouterParams() - Get the query parameters with
getRouterQuery()
- Get the current pathname with
- Supports dynamic URLs (E.g.
/help/:section/:topic) withgetRouterParams() - Supports URL prefixing using
Router.basename. (Useful for sites like Github Pages)
QuickStart
import van from "vanjs-core";
import { Router, Link, getRouterParams, navigate } from "vanjs-routing";
const { div, p, button } = van.tags;
function App() {
return Router({
basename: "vanjs-routing", // Optional base name (All links are now prefixed with '/vanjs-routing')
routes: [
{ path: "/", component: HomeComponent },
{ path: "/about", component: AboutComponent },
{ path: "/help/:section", component: HelpComponent }
]
});
}
function HomeComponent() {
return div(p("Home"), Link({ href: "/about?foo=bar" }, "Goto About"), Link({ href: "/help/profile" }, "Goto Help"));
}
function AboutComponent() {
return div(p("About"), Link({ href: "/" }, "Back to Home"));
}
function HelpComponent() {
van.derive(() => {
console.log(getRouterParams()); // { section: "profile" }
});
return div(
p("Help"),
Link({ href: "/" }, "Back to Home"),
button({ onclick: () => navigate("/") }, "Back to Home (Imperative navigation)")
);
}
van.add(document.body, App());API Reference
Router
- The
Routercomponent which you use to define your routes - Each
routeis an object with apathandcomponent- The
componentis a function returning anHTMLElement
- The
import { Router } from "vanjs-routing";
Router({
basename?: string,
routes: Array <{
path: string,
component: () => HTMLElement
}>
});Link
- The
Linkextends thevan.tags.acomponent to tap into the router's internal state for navigation Linkis a drop-in replacement forvan.tags.a- If
replaceis set totrue, the current route will be replaced with the Link'shrefwhen clicked
import { Link } from "vanjs-routing";
Link({
replace?: boolean
// Additional van.tags.a props
});Navigate
- The
navigatefunction is useful in areas whereLinkcannot be used. For example in a function or side-effect - If
replaceis set totrue, the current route will be replaced withhrefinstead of pushing to the history stack.
import { navigate } from "vanjs-routing";
navigate(
href,
options ?: {
replace?: boolean
}
)Router state helpers
getRouterPathname()returns the current pathnamegetRouterParams()returns the parameter values in a dynamic routegetRouterQuery()returns the query parameters
import { getRouterPathname, getRouterParams, getRouterQuery } from "vanjs-routing";
// Route path: /home/:section/:topic
// Current URL: https://hello.com/home/learning/science?tab=intro
console.log(getRouterPathname()); // "/home/learning/science"
console.log(getRouterParams()); // { section: "learning", topic: "science" }
console.log(getRouterQuery()); // { tab: "intro" }Contributors
Change Log
1.1.3- Update
package.jsonmetadata and README documentation
- Update
1.1.2- Update README documentation
1.1.0- Added
basenameprop toRoutercomponent.
- Added