Skip to content

Commit

Permalink
Dynamic route declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Lelaisant committed Oct 18, 2019
1 parent 09e3f1e commit 55082e4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ yarn add svelters-router

## Usage

### Declare route with Route component

```svelte
<script>
import { Router, Route } from 'svelters-router'
Expand All @@ -36,6 +38,34 @@ yarn add svelters-router
</Router>
```

### Declare route programmatically

```svelte
<script>
import { Router, Route } from 'svelters-router'
import Home from './Home.svelte'
import Users from './Users.svelte'
import User from './User.svelte'
let routes = [
{
path: "/users",
component: Users
},
{
path: "/users/:id",
component: User
}
]
</script>
<Router {routes}>
<Route path="/" component={Home} />
</Router>
```

:bulb: Note that you can combine the two ways of declaring routes.

### Get route parameters in component

Route parameters are compiled by `svelters-router` and given to defined component through a prop named `routeParams`.
Expand Down
6 changes: 6 additions & 0 deletions src/components/Router.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<script>
import Route from './Route.svelte'
import { location } from '../store'
export let routes = []
location.update(() => window.location)
</script>

{#each routes as route}
<Route path={route.path} component={route.component} />
{/each}
<slot></slot>

0 comments on commit 55082e4

Please sign in to comment.