Caveat - the info in my reply probably may not be valid as SvelteKit matures, but from experiments I have done thus far, this works:
Parameter based routing is similar to the sveltejs/sapper-template
. You should learn how Sapper does it first. Lets say I have a route blog
with a single param slug (/blog/page-1 & /blog/page-2)
- Create a route component in routes/blog named
[slug].svelte
- Copy the content from the
sveltejs/sapper-template
example.
- Rename the
preload
function to load
with a single parameter such as ctx
- Alter the return object to append your slug property to
props
export async function load(ctx) {
let slug = ctx.page.params.slug
return { props: { slug }}
}
If your blog has multiple params for the slug (/blog/2021/01/29/this-is-a-slug), you can remove [slug].svelte
and create a file name [...data].svelte
and change your load method to:
export async function load(ctx) {
let [year, month, day, slug] = ctx.page.params.data;
return { props: { data: { year, month, day, slug }}}
}
Don't forget to define your data property as an object. Here's a typescript example:
<script lang="ts">
export let data: { slug: string, year: number, month: number, day: number };
</script>
From there use the values as {data.slug}
, etc
Happy hacking
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…