2022-04-08 16:54:55 +02:00
|
|
|
---
|
|
|
|
title: <svelte:element>
|
|
|
|
---
|
|
|
|
|
|
|
|
Sometimes we don't know in advance what kind of DOM element to render. `<svelte:element>` comes in handy here. Instead of a sequence of `if` blocks...
|
|
|
|
|
2023-04-02 17:24:33 +02:00
|
|
|
```svelte
|
2022-04-08 16:54:55 +02:00
|
|
|
{#if selected === 'h1'}
|
|
|
|
<h1>I'm a h1 tag</h1>
|
|
|
|
{:else if selected === 'h3'}
|
|
|
|
<h3>I'm a h3 tag</h3>
|
|
|
|
{:else if selected === 'p'}
|
|
|
|
<p>I'm a p tag</p>
|
|
|
|
{/if}
|
|
|
|
```
|
|
|
|
|
|
|
|
...we can have a single dynamic component:
|
|
|
|
|
2023-04-02 17:24:33 +02:00
|
|
|
```svelte
|
2022-04-08 16:54:55 +02:00
|
|
|
<svelte:element this={selected}>I'm a {selected} tag</svelte:element>
|
|
|
|
```
|
|
|
|
|
2023-04-02 17:24:33 +02:00
|
|
|
The `this` value can be any string, or a falsy value — if it's falsy, no element is rendered.
|