Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

jikkai/svelte-router

 
 

Repository files navigation

Svelte Router

version license

Router component for Svelte

This branch is for Svelte v1.8.1+. For usage with Svelte v1.8.0-, see the 0.1.x branch.

Installation

Using npm:

npm install --save svelte-router

Using yarn:

yarn add svelte-router

Examples

<div>
  <RouterLink to="/">Home</RouterLink>
  <RouterLink to="/welcome">Welcome</RouterLink>
  <div id="app"></div>
</div>

<script>
  import { Store } from 'svelte/store'
  import SvelteRouter from 'svelte-router'
  import Home from './Home.html'
  import Welcome from './Welcome.html'
  import Animal from './Animal.html'

  const router = new SvelteRouter({
    mode: 'hash',
    routes: {
      '/': Home,
      '/welcome': Welcome,
      '/animal': {
        Component: Animal,
        props: {
          store: new Store({
            animal: 'dog',
            sheep: 'baaah',
            moo: {
              cow: true,
              foo: 'bar'
            }
          }),
          data: {
            qwert: 'asdf'
          }
        }
      }
    }
  })

  export default {
    oncreate () {
      router.create('#app')
    },

    ondestroy () {
      router.destroy()
    },

    components: {
      RouterLink: SvelteRouter.RouterLink
    }
  }
</script>

<style>
  .router-link-active {
    color: red;
  }
</style>

API

SvelteRouter

  • push(path: string)
  • replace(path: string)
  • go(n: number)
  • listen(fn: function)

RouterLink

  • replace: boolean
  • match: string

Contributors