Sveltekit load function issue
Solution 1:
the error states page in load functions has been replaced by url and params
a quick search in the documentation as noted here
yields this snippet
export async function load({ params, fetch, session, stuff }) {
const url = `/blog/${params.slug}.json`;
const res = await fetch(url);
if (res.ok) {
return {
props: {
article: await res.json()
}
};
}
return {
status: res.status,
error: new Error(`Could not load ${url}`)
};
}
you can directly use the params
within load()
you can refactor your code to
<script context="module">
export async function load({params}) {
const id = params.id;
const url = `https://pokeapi.co/api/v2/pokemon/${id}`;
const res = await fetch(url);
const pokeman = await res.json();
return {props: {pokeman}};
}
</script>
<script>
export let pokeman;
</script>
<h1>{pokeman.name}</h1>