Closed LucaGabi closed 4 years ago
The router contains only routes resolving code, you should handle click yourself. It is petty easy:
<!doctype html>
<html>
<head>
<title>Router Demo</title>
</head>
<body>
<nav>
<a href="/">Home</a> |
<a href="/demo">Demo</a> |
<a href="/page">Page</a>
</nav>
<main>
<h1>Home</h1>
</main>
<script src="https://unpkg.com/universal-router/universal-router.js"></script>
<script>
const router = new UniversalRouter([
{ name: 'home', path: '', action: () => '<h1>Home</h1>' },
{ name: 'demo', path: '/demo', action: () => '<h1>Demo</h1>' },
{ name: 'page', path: '/page', action: () => '<h1>Page</h1>' }
])
const container = document.getElementsByTagName('main')[0]
const render = (pathname) => router.resolve(pathname).then(html => {
container.innerHTML = html
})
window.addEventListener('click', event => {
if (event.target.tagName === 'A') {
event.preventDefault()
render(event.target.pathname)
}
})
</script>
</body>
</html>
Demo: https://frenzzy.github.io/test/router-demo.html See other examples here: https://github.com/kriasoft/universal-router/issues/72#issuecomment-321491693 and here: https://github.com/kriasoft/universal-router/issues/80#issuecomment-281077903
Thanks, after looking in the source I got that .. I've done exactly the same + some history handling.
I'm submitting a ..BUG?.
With the following code when link is clicked the router dose not activate, instead it dose browser get request. Using VSCode liveserver at 127.0.01:5500