Skip to content

Instantly share code, notes, and snippets.

@arnu515
Created January 17, 2021 15:22
Show Gist options
  • Save arnu515/b37fd5eb84c6917f9f3c7d6085498ad4 to your computer and use it in GitHub Desktop.
Save arnu515/b37fd5eb84c6917f9f3c7d6085498ad4 to your computer and use it in GitHub Desktop.
Basic routing using PageJS in svelte
<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 }} />
<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