Skip to content

iuroc/vanjs-router

Repository files navigation

vanjs-router

Simple Frontend Routing Component Based on Van.js.

This is version 2. For version 1 documentation, please click here.

Features

  • Supports both string and regex matching.
  • Supports setting page display delay.
  • Supports configuring events for the first route match (onFirst) and subsequent route matches (onLoad).
  • Implemented using TypeScript.
  • Simple API.

Quick Start

npm install vanjs-router
<script src="https://cdn.jsdelivr.net/npm/vanjs-router@latest/dist/vanjs-router.min.js"></script>
<script>
  const { Route, goto } = router;
  const { div, button } = van.tags;

  const Home = () =>
    Route({
      rule: "home",
      Loader() {
        return div(
          "This Is Home Page.",
          button({ onclick: () => goto("about") }, "Go To About")
        );
      },
      onFirst() {
        console.log("home onfirst");
      },
      onLoad() {
        console.log("home onload");
      },
    });

  const About = () =>
    Route({
      rule: "about",
      delayed: true,
      Loader() {
        return div(
          "This Is About Page.",
          button({ onclick: () => goto("home") }, "Go To Home")
        );
      },
      onLoad() {
        this.show();
      },
    });

  van.add(document.body, Home(), About());
</script>