The Router class provides client-side routing functionality for single page applications. It manages navigation between different views without full page reloads.
- Simple route registration
- History API integration
- Automatic route handling
- URL path matching
import { createRouter } from 'cdn'; // you cane get it from the readme file
// Create router instance
const router = createRouter();
// Initialize router
router.init();
// Add routes
router.addRoute('/', homeComponent);
router.addRoute('/about', aboutComponent);
router.addRoute('/contact', contactComponent);
// Navigate programmatically
router.navigate('/about');const router = new Router(); // don't forget to import the Router example: import {Router} from 'cdn';Creates a new router instance and initializes route handling.
path: String - URL path to matchcomponent: Function - Component render function to call when route matches
Registers a new route and associated component.
path: String - Path to navigate to
Programmatically navigate to a route.
Internal method that handles route changes and renders appropriate component.
The router listens for popstate events to handle browser back/forward navigation.
Works in all modern browsers that support the History API.