Last active
November 27, 2017 20:10
-
-
Save timhall/e9a5bd43c541cedf6c39e27b437fe2cf to your computer and use it in GitHub Desktop.
svelte-router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Route path="/" exact> | |
Home | |
</Route> | |
<Route path="/a"> | |
A | |
<Route path="/a/nested">Nested</Route> | |
</Route> | |
<Route path="/b/:id"> | |
B {{$router.params.id}} | |
</Route> | |
<script> | |
import { Route } from 'svelte-router'; | |
export default { | |
components: { Route } | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="app"></div> | |
<script> | |
import { Store } from 'svelte/store'; | |
import { Router } from 'svelte-router'; | |
import App from './App.html' | |
const store = new Store(); | |
const router = new Router(); | |
router.connect(store); | |
const app = new App({ | |
target: document.getElementById('app'), | |
store | |
}); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{#if active}} | |
<slot /> | |
{{/if}} | |
<script> | |
export default { | |
data: () => ({ path: '', exact: false }) | |
computed: { | |
active: (path, exact, router, $router) => { | |
if (router) return router.matches(path, exact); | |
if ($router) return $router.matches(path, exact); | |
return false; | |
} | |
} | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export { default as Route } from './Route.html'; | |
export class Router { | |
connect(store) { | |
// store.set({ router }) on each URL change | |
} | |
matches(path, exact = false) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment