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

Commit f9e63f0

Browse files
committed
feat: support passing props to route components
1 parent 80585d7 commit f9e63f0

File tree

8 files changed

+72
-39
lines changed

8 files changed

+72
-39
lines changed

babel.config.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,5 @@ module.exports = {
1010
]
1111
}
1212
}]
13-
],
14-
plugins: [
15-
'@babel/plugin-transform-spread',
16-
'@babel/plugin-proposal-class-properties'
1713
]
1814
}

example/components/menu.svelte

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
<span>Getting Started</span>
1212
<ul>
1313
<li>
14-
<Link to="/guide/basic-usage" replace>Basic Usage</Link>
14+
<Link to="/guide/basic-usage">Basic Usage</Link>
15+
</li>
16+
<li>
17+
<Link to="/guide/passing-props">Passing Props</Link>
1518
</li>
1619
<li>
1720
<Link to="/guide/api">API</Link>
@@ -24,7 +27,7 @@
2427
<span>Component</span>
2528
<ul>
2629
<li>
27-
<Link to="/component/link" replace>Link</Link>
30+
<Link to="/component/link">Link</Link>
2831
</li>
2932
</ul>
3033
</div>

example/docs/guide/PassingProps.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Passing Props
2+
3+
Passing props to route components by using `props`.
4+
5+
## Foo.svelte
6+
7+
```html
8+
<h1>Hello {title}</h1>
9+
10+
<script>
11+
export let title
12+
</script>
13+
```
14+
15+
## App.svelte
16+
17+
```html
18+
<div>
19+
<div use:create></div>
20+
</div>
21+
22+
<script>
23+
import SvelteRouter from 'svelte-router'
24+
import Foo from './Foo.svelte'
25+
26+
function create (node) {
27+
const router = new SvelteRouter({
28+
target: node,
29+
routes: [{
30+
path: '/foo',
31+
component: Foo,
32+
props: {
33+
title: 'Foo'
34+
}
35+
}]
36+
})
37+
38+
return {
39+
destroy () {
40+
router.destroy()
41+
}
42+
}
43+
}
44+
</script>
45+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<section>
2+
{@html content}
3+
</section>
4+
5+
<script>
6+
import content from '../../docs/guide/PassingProps'
7+
</script>

example/routes.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Introduction from './pages/Introduction'
22
import Installation from './pages/Installation'
33
import BasicUsage from './pages/guide/BasicUsage'
4+
import PassingProps from './pages/guide/PassingProps'
45
import API from './pages/guide/API'
56
import Link from './pages/component/Link'
67
import NotFound from './pages/NotFound'
@@ -14,6 +15,12 @@ export default [{
1415
}, {
1516
path: '/guide/basic-usage',
1617
component: BasicUsage
18+
}, {
19+
path: '/guide/passing-props',
20+
component: PassingProps,
21+
props: {
22+
foo: 'bar'
23+
}
1724
}, {
1825
path: '/guide/api',
1926
component: API

package-lock.json

Lines changed: 1 addition & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svelte-router",
3-
"version": "3.0.0-alpha.1",
3+
"version": "3.0.0-alpha.2",
44
"description": "A simple router for Svelte",
55
"main": "lib/svelte-router.js",
66
"author": "Jikkai Xiao <sonne@asaki.me>",
@@ -36,8 +36,6 @@
3636
},
3737
"devDependencies": {
3838
"@babel/core": "^7.5.5",
39-
"@babel/plugin-proposal-class-properties": "^7.5.5",
40-
"@babel/plugin-transform-spread": "^7.2.2",
4139
"@babel/preset-env": "^7.5.5",
4240
"alius": "0.0.1",
4341
"babel-eslint": "^10.0.2",

src/router.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ class SvelteRouter {
2929
this.$content = null
3030
this.target = typeof target === 'string' ? document.querySelector(target) : target
3131
this.routes = routes
32-
this.$listener = SvelteRouter.history.listen(this.handleRouteChange.bind(this))
32+
this.$listener = history.listen(this.handleRouteChange.bind(this))
3333

34-
this.handleRouteChange(SvelteRouter.history.location)
34+
this.handleRouteChange(history.location)
3535
}
3636

3737
destroy () {
38-
if (this.listener) {
38+
if (this.$listener) {
3939
this.$listener()
4040
this.$listener = null
4141
}
@@ -54,10 +54,11 @@ class SvelteRouter {
5454

5555
if (matchedRoute && matchedRoute.component) {
5656
if (this.$content) this.$content.$destroy()
57-
const Component = matchedRoute.component
57+
const { component: Component, props } = matchedRoute
5858

5959
this.$content = new Component({
60-
target: this.target
60+
target: this.target,
61+
props
6162
})
6263
}
6364
}

0 commit comments

Comments
 (0)