Home

Awesome

Svelte Router

<!-- prettier-ignore -->

package version npm downloads ci

Before you start, I'd highly recommend checking out SvelteKit first. It has a flexible filesystem-based router officially supported by the Svelte team.

An easy-to-use SPA router for Svelte.

Comparison with other routers

Features

Not Supported

Quick Start

Install @bjornlu/svelte-router:

$ npm install @bjornlu/svelte-router

Define routes:

// main.js

import { initPathRouter } from '@bjornlu/svelte-router'
import App from './App.svelte'
import Home from './Home.svelte'

// Use `initHashRouter` for hash mode
initPathRouter([{ path: '/', component: Home }])

const app = new App({
  target: document.body
})

export default app

Render routes and links:

<!-- App.svelte -->

<script>
  import { RouterView, Link } from '@bjornlu/svelte-router'
</script>

<main>
  <nav>
    <Link to="/">Home</Link>
  </nav>
  <RouterView />
</main>

Done!

Documentation

Ready to learn more? The documentation is split into two parts:

Examples

Contributing

All contributions are welcomed. Check out CONTRIBUTING.md for more details.

License

MIT