Tested against Svelte 5.1.3. This is a library for SPA's and does not support SSR at this time.
<script lang="ts">
import { Router } from "@teleology/svelte-router"
import { Test } from "./routes/Test.svelte"
let isAuthenticated = true;
const routes = [
{
path: "*",
// lazy loaded
component: () => import("./lib/routes/NotFound.svelte"),
},
{
path: "/test"
// non-lazy load
component: Test,
},
{
path: "/user",
component: () => import("./lib/routes/UserProfile.svelte"),
layout: () => import("./lib/pages/UserLayout.svelte"),
redirect: "/",
guard: () => isAuthenticated
}
]
</script>
<Router {routes}>
Key | Type | Description |
---|---|---|
path |
String |
A pathing string |
component |
|
An imported SvelteComponent or a lazy-loaded component |
layout |
|
An imported SvelteComponent or a lazy-loaded component. Must have {@render children}!! |
redirect |
String |
An alternate url if the guard function is not true |
guard |
() => Boolean |
A synchronous function that returns whether the route should be navigated to |
<script lang="ts">
import { push, type RouteDetails } from "@teleology/svelte-router";
interface Props {
route: RouteDetails;
}
let { route }: Props = $props();
</script>
<h1>You landed on {route.pathname}</h1>
Route details are extends from the browser Location object
Key | Type | Description |
---|---|---|
params |
Object |
The key-value object of url params |
search |
Object
|
The key-value object of search params |