Skip to content

Commit 8c3856e

Browse files
author
Rich Harris
committed
tweak
1 parent 7d7157f commit 8c3856e

File tree

2 files changed

+13
-7
lines changed
  • content/tutorial/03-advanced-svelte/09-special-elements/02-svelte-component

2 files changed

+13
-7
lines changed

content/tutorial/03-advanced-svelte/09-special-elements/02-svelte-component/README.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
title: <svelte:component>
33
---
44

5-
A component can change its category altogether with `<svelte:component>`. Instead of a sequence of `if` blocks...
5+
A component can change its type altogether with `<svelte:component>`. In this exercise, we want to show `RedThing.svelte` if the `color` is `red`, `GreenThing.svelte` if it's `green`, and so on.
6+
7+
We _could_ do this with a sequence of `if` blocks...
68

79
```svelte
810
/// file: App.svelte
@@ -15,11 +17,17 @@ A component can change its category altogether with `<svelte:component>`. Instea
1517
{/if}
1618
```
1719

18-
...we can have a single dynamic component:
20+
...but it's a little cumbersome. Instead, we can create a single dynamic component:
1921

2022
```svelte
2123
/// file: App.svelte
22-
<svelte:component this={selected.component}/>
24+
<select bind:value={selected}>
25+
{#each options as option}
26+
<option value={option}>{option.color}</option>
27+
{/each}
28+
</select>
29+
30+
+++<svelte:component this={selected.component}/>+++
2331
```
2432

2533
The `this` value can be any component constructor, or a falsy value — if it's falsy, no component is rendered.

content/tutorial/03-advanced-svelte/09-special-elements/02-svelte-component/app-a/src/lib/App.svelte

+2-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
{#if selected.color === 'red'}
2222
<RedThing />
23-
{:else if selected.color === 'green'}
24-
<GreenThing />
25-
{:else if selected.color === 'blue'}
26-
<BlueThing />
23+
{:else}
24+
<p>TODO others</p>
2725
{/if}

0 commit comments

Comments
 (0)