@bjornlu/svelte-router
TypeScript icon, indicating that this package has built-in type declarations

0.5.0 • Public • Published

Svelte Router

package version npm downloads ci

An easy-to-use SPA router for Svelte.

Comparison with other routers

Features

  • Super simple API
  • Support hash and path based navigation
  • Nested routes
  • Redirects and navigation guards (with async support)
  • Lazy loading routes
  • Auto base tag navigation

Not Supported

  • Server-side rendering (SSR) - Use Sapper instead
  • Relative navigations - Use absolute path and dynamic syntax instead

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:

  • Guide: Covers common usage to advanced topics
  • API reference: Covers the structure of the API

Examples

Contributing

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

License

MIT

Dependents (0)

Package Sidebar

Install

npm i @bjornlu/svelte-router

Weekly Downloads

2

Version

0.5.0

License

MIT

Unpacked Size

72.5 kB

Total Files

24

Last publish

Collaborators

  • bjornlu
  • bluwy