Created
January 17, 2021 15:22
-
-
Save arnu515/b37fd5eb84c6917f9f3c7d6085498ad4 to your computer and use it in GitHub Desktop.
Basic routing using PageJS in svelte
This file contains hidden or 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
<script lang="ts"> | |
import page from "page"; | |
import { setContext } from "svelte"; | |
import { parse } from "qs"; | |
import Index from "./routes/index.svelte"; | |
export let apiUrl: string; | |
setContext<string>("apiUrl", apiUrl); | |
let component: any, query: any, params: any; | |
/** | |
* Adds a route in PageJS. | |
* @param path Route path | |
* @param c The Svelte Component | |
*/ | |
function route(path: string, c: any) { | |
page(path, (ctx) => { | |
query = parse(ctx.querystring); | |
params = ctx.params; | |
component = c; | |
}); | |
} | |
route("/", Index); | |
page.start(); | |
</script> | |
<svelte:component this={component} {...{ query, params }} /> |
This file contains hidden or 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
<script lang="ts"> | |
export let query = {}; | |
console.log(query); | |
</script> | |
<h1>Hello, world</h1> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment