mirror of https://github.com/sveltejs/svelte
parent
41793b6694
commit
1ac5b51f39
@ -0,0 +1,15 @@
|
|||||||
|
<script>
|
||||||
|
let cats = [
|
||||||
|
{ id: 'J---aiyznGQ', name: 'Keyboard Cat' },
|
||||||
|
{ id: 'z_AbfPXTKms', name: 'Maru' },
|
||||||
|
{ id: 'OUtn3pvWmpg', name: 'Henri The Existential Cat' }
|
||||||
|
];
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<h1>The Famous Cats of YouTube</h1>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<!-- open each block -->
|
||||||
|
<li><a target="_blank" href="https://www.youtube.com/watch?v={cat.id}">{cat.name}</a></li>
|
||||||
|
<!-- close each block -->
|
||||||
|
</ul>
|
@ -0,0 +1,15 @@
|
|||||||
|
<script>
|
||||||
|
let cats = [
|
||||||
|
{ id: 'J---aiyznGQ', name: 'Keyboard Cat' },
|
||||||
|
{ id: 'z_AbfPXTKms', name: 'Maru' },
|
||||||
|
{ id: 'OUtn3pvWmpg', name: 'Henri The Existential Cat' }
|
||||||
|
];
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<h1>The Famous Cats of YouTube</h1>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
{#each cats as { id, name }}
|
||||||
|
<li><a target="_blank" href="https://www.youtube.com/watch?v={id}">{name}</a></li>
|
||||||
|
{/each}
|
||||||
|
</ul>
|
@ -0,0 +1,17 @@
|
|||||||
|
---
|
||||||
|
title: Each blocks
|
||||||
|
---
|
||||||
|
|
||||||
|
If you need to loop over lists of data, use an `each` block:
|
||||||
|
|
||||||
|
```html
|
||||||
|
<ul>
|
||||||
|
{#each cats as cat}
|
||||||
|
<li><a target="_blank" href="https://www.youtube.com/watch?v={cat.id}">{cat.name}</a></li>
|
||||||
|
{/each}
|
||||||
|
</ul>
|
||||||
|
```
|
||||||
|
|
||||||
|
> The expression (`cats`, in this case) can be any array or array-like object (i.e. it has a `length` property). You can loop over generic iterables with `each [...iterable]`.
|
||||||
|
|
||||||
|
If you prefer, you can use destructuring — `each cats as { id, name }` — and replace `cat.id` and `cat.name` with `id` and `name`.
|
Loading…
Reference in new issue