Complete the code to create a basic SvelteKit page component.
<script> export let name = "World"; </script> <h1>Hello, [1]!</h1>
The variable name is exported and used inside the template to display the greeting.
Complete the code to navigate to a new page using SvelteKit's goto function.
<script> import { goto } from '$app/navigation'; function goToAbout() { goto('[1]'); } </script> <button on:click={goToAbout}>About</button>
The goto function navigates to the specified path. Here, it should go to the /about page.
Fix the error in the SvelteKit load function to fetch data from an API.
export async function load({ fetch }) {
const res = await fetch('[1]');
const data = await res.json();
return { props: { data } };
}The fetch inside load can call external URLs. The full URL is needed for external APIs.
Fill both blanks to create a reactive statement that updates double when count changes.
<script> let count = 0; let double; $: [1] = [2] * 2; </script>
The reactive statement uses $: double = count * 2; to update double whenever count changes.
Fill all three blanks to create a SvelteKit form that submits data and handles the response.
<script> import { invalidate } from '$app/navigation'; async function handleSubmit(event) { event.preventDefault(); const formData = new FormData(event.target); const response = await fetch('[1]', { method: '[2]', body: formData }); if (response.ok) { await invalidate('[3]'); } } </script> <form on:submit={handleSubmit} aria-label="Contact form"> <input name="email" type="email" required aria-required="true" placeholder="Your email" /> <button type="submit">Send</button> </form>
The form submits to /contact using POST. After submission, invalidate('/contact') refreshes the data.