You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/docs/02-template-syntax/06-component-directives.md
+27-4
Original file line number
Diff line number
Diff line change
@@ -8,13 +8,34 @@ title: Component directives
8
8
on:eventname={handler}
9
9
```
10
10
11
-
Components can emit events using [createEventDispatcher](/docs/svelte#createeventdispatcher), or by forwarding DOM events. Listening for component events looks the same as listening for DOM events:
11
+
Components can emit events using [`createEventDispatcher`](/docs/svelte#createeventdispatcher) or by forwarding DOM events.
12
+
13
+
```svelte
14
+
<!-- SomeComponent.svelte -->
15
+
<script>
16
+
import { createEventDispatcher } from 'svelte';
17
+
18
+
const dispatch = createEventDispatcher();
19
+
</script>
20
+
21
+
<!-- programmatic dispatching -->
22
+
<button on:click={() => dispatch('hello')}>
23
+
one
24
+
</button>
25
+
26
+
<!-- declarative event forwarding -->
27
+
<button on:click>
28
+
two
29
+
</button>
30
+
```
31
+
32
+
Listening for component events looks the same as listening for DOM events:
12
33
13
34
```svelte
14
35
<SomeComponent on:whatever={handler} />
15
36
```
16
37
17
-
As with DOM events, if the `on:` directive is used without a value, the component will _forward_ the event, meaning that a consumer of the component can listen for it.
38
+
As with DOM events, if the `on:` directive is used without a value, the event will be forwarded, meaning that a consumer can listen for it.
18
39
19
40
```svelte
20
41
<SomeComponent on:whatever />
@@ -92,6 +113,8 @@ You can bind to component props using the same syntax as for elements.
92
113
<Keypad bind:value={pin} />
93
114
```
94
115
116
+
While Svelte props are reactive without binding, that reactivity only flows downward into the component by default. Using `bind:property` allows changes to the property from within the component to flow back up out of the component.
0 commit comments