Vanilla UI Router
Simple vanilla JavaScript router to be used inside a single page app to add routing capabilities.
The router comes with zero dependencies and can be used with any other libraries. It's based on the hashchange-Event.
Installation
$ npm install --save vanilla-ui-router
As UMD module this runs everywhere (ES6 modules, CommonJS, AMD and with good ol’ globals).
Usage
Let's assume your initial markup has the following structure:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles.min.css" />
</head>
<body>
<main id="app"></main>
<script src="bundle.js"></script>
</body>
</html>
Then you could configure the router with the following JavaScript:
import {createRouter} from 'vanilla-ui-router';
const router = createRouter(document.getElementById('app'));
router
.addRoute('', () => {
router.navigateTo('home');
})
.addRoute('home', (domEntryPoint) => {
domEntryPoint.textContent = 'I am the home route.';
})
.addRoute('about/:aboutId/:editable', (domEntryPoint, routeParams) => {
console.log('I am the about route.');
console.log(routeParams);
})
.addRoute('route-with-template-url', {
templateUrl: 'path/to/template.html'
})
.addRoute('route-with-template-string/:id', {
templateString: '<p>Lorem ipsum dolor.</p>',
routeHandler: (domEntryPoint, routeParams) => {
}
})
.addRoute('route-with-template-id/:id', {
templateId: 'template42'
})
.otherwise(() => {
console.log('I am the otherwise route');
router.navigateTo('404');
});
License
Please be aware of the licenses of the components we use in this project. Everything else that has been developed by the contributions to this project is under MIT License.