Skip to content

jamen/svelte-router

master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 

@jamen/svelte-router

Svelte router using a store and components.

Usage

First setup a view with <Router>:

<script>
    import { Router } from '@jamen/svelte-router'
    import * as pages from './pages.js'

    const routes = {
        '/': pages.Home,
        '/contact': pages.Contact,
        404: pages.Lost,
        // ...
    }
</script>

<Router {routes} />

Then you can use <Link> to change the view:

<script>
    import { Link } from '@jamen/svelte-router'
</script>

<nav>
    <Link href='/'>Home</Link>
    <Link href='/contact'>Contact</Link>
</nav>

And you can use the router store to have your own routing:

<script>
    import { router } from '@jamen/svelte-router'
</script>

{#if $router.query.name}
    <h1>Hello {$router.query.name}!</h1>
{/if}

<p>You visited {$router.path}.</p>

If you want to use your own store, then both Router and Link accept a router to change the store:

<script>
    import { Router, Link } from '@jamen/svelte-router'
    import { custom } from '../stores.js'
    // ...
</script>

<Router router={custom} {routes} />
<Link router={custom} href='/'>Home</Link>

With this you may want your own link component:

<script>
    import { Link } from '@jamen/svelte-router'
    import { custom } from '../stores.js'
</script>

<Link router={custom} {..$$props}><slot></slot></Link>

About

Svelte router using a store and components.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published