DEV Community

Lam Hoang
Lam Hoang

Posted on • Originally published at simplecheatsheet.com

Forwarding Events in Svelte

// Widget.svelte
<script>
  import { createEventDispatcher } from "svelte";
  const dispatch = createEventDispatcher();
</script>
<button on:click={() => dispatch('message', { text: 'Hello!' })} />
<button on:click>Press me</button>

// App.svelte
<script>
import Widget from '.Widget.svelte'
</script>

<Widget 
  on:click={() => alert('I was clicked')} 
  on:message={e => alert(e.detail.text)}>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)