admin管理员组文章数量:1122832
In Svelte 5, how can I wrap each child with a tag like the example below?
{#if children}
{#each children as child}
<button
onclick={() => {
handleClick();
}}
>
{@render child?.()}
</button>
{/each}
{/if}
In Svelte 5, how can I wrap each child with a tag like the example below?
{#if children}
{#each children as child}
<button
onclick={() => {
handleClick();
}}
>
{@render child?.()}
</button>
{/each}
{/if}
Share
Improve this question
asked Nov 21, 2024 at 21:49
BaboyibanBaboyiban
133 bronze badges
1 Answer
Reset to default 1You can't.
Snippets are fragments made up of components, logic blocks and DOM that can contain absolutely anything, they are not iterable.
You could pass a data array or multiple snippets, both of which could be iterated.
Passing data example:
<script>
import List from './List.svelte';
const items = [1, 2, 3];
</script>
<List {items}>
{#snippet itemTemplate(item)}
Item: {item}
{/snippet}
</List>
<!-- List.svelte -->
<script>
const { items, itemTemplate } = $props();
</script>
<ul>
{#each items as item}
<li>{@render itemTemplate(item)}</li>
{/each}
</ul>
Alternatively, wrap every child in a separate component along the lines of:
<List>
<ListItem>1</ListItem>
<ListItem>2</ListItem>
...
</List>
Then this component can take of anything that needs to be added around the items. This is of course more verbose and the user needs to know that this should be added.
本文标签: sveltekitHow to Wrap Each Child with a Tag in Svelte 5Stack Overflow
版权声明:本文标题:sveltekit - How to Wrap Each Child with a Tag in Svelte 5? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736306984a1933155.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论