Complete the code to forward the click event from the child component.
<script> import { createEventDispatcher } from 'svelte'; const dispatch = createEventDispatcher(); function handleClick(event) { dispatch('[1]', event); } </script> <button on:click={handleClick}>Click me</button>
The child component forwards the click event by dispatching it with the same name.
Complete the parent component code to listen to the forwarded event.
<script> import Child from './Child.svelte'; function onChildClick(event) { alert('Child clicked!'); } </script> <Child [1]={onChildClick} />
In Svelte, to listen to an event, use on:eventname syntax. Here, on:click listens to the forwarded click event.
Fix the error in the child component to forward a custom event named 'select'.
<script> import { createEventDispatcher } from 'svelte'; const dispatch = createEventDispatcher(); function handleSelect() { dispatch([1], { id: 1 }); } </script> <button on:click={handleSelect}>Select</button>
The event name must be a string. Using quotes like 'select' is correct.
Fill both blanks to forward a 'submit' event with the original event object.
<script> import { createEventDispatcher } from 'svelte'; const dispatch = createEventDispatcher(); function handleSubmit(event) { dispatch([1], [2]); } </script> <form on:submit|preventDefault={handleSubmit}> <button type="submit">Submit</button> </form>
The event name is 'submit' as a string, and the original event object is passed as the second argument.
Fill all three blanks to forward a custom event with a detail object containing 'id' and 'name'.
<script> import { createEventDispatcher } from 'svelte'; const dispatch = createEventDispatcher(); function sendData() { dispatch([1], { [2]: 42, [3]: 'Alice' }); } </script> <button on:click={sendData}>Send Data</button>
The event name is 'data'. The detail object has keys 'id' and 'name' with values 42 and 'Alice'.