Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jamen/svelte-router

Svelte router using a store and components.
https://github.com/jamen/svelte-router

Last synced: 16 days ago
JSON representation

Svelte router using a store and components.

Awesome Lists containing this project

README

        

# @jamen/svelte-router

Svelte router with a store and components.

**NOTE:** Consider using [SvelteKit](https://kit.svelte.dev/) with [@sveltejs/adapter-static](https://github.com/sveltejs/kit/tree/master/packages/adapter-static) instead. It has routing.

## Usage

First setup a view with ``:

```html

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

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

```

Then you can use `` to change the view:

```html

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

Home
Contact

```

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

```html

import { router } from '@jamen/svelte-router'

{#if $router.query.name}

Hello {$router.query.name}!


{/if}

You visited {$router.path}.


```

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

```html

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

Home
```

With this you may want your own link component:

```html

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

```