mirror of https://github.com/sveltejs/svelte
29 lines
475 B
29 lines
475 B
6 years ago
|
---
|
||
|
title: Slots
|
||
|
---
|
||
|
|
||
|
Just like elements can have children...
|
||
|
|
||
2 years ago
|
```svelte
|
||
6 years ago
|
<div>
|
||
|
<p>I'm a child of the div</p>
|
||
|
</div>
|
||
|
```
|
||
|
|
||
|
...so can components. Before a component can accept children, though, it needs to know where to put them. We do this with the `<slot>` element. Put this inside `Box.svelte`:
|
||
|
|
||
2 years ago
|
```svelte
|
||
6 years ago
|
<div class="box">
|
||
2 years ago
|
<slot />
|
||
6 years ago
|
</div>
|
||
|
```
|
||
|
|
||
|
You can now put things in the box:
|
||
|
|
||
2 years ago
|
```svelte
|
||
6 years ago
|
<Box>
|
||
|
<h2>Hello!</h2>
|
||
|
<p>This is a box. It can contain anything.</p>
|
||
|
</Box>
|
||
2 years ago
|
```
|