xsl 模板 循环_苗条的模板:循环

xsl 模板 循环

In Svelte templates you can create a loop using the {#each}{/each} syntax:

在Svelte模板中,您可以使用{#each}{/each}语法创建循环:

<script>
let goodDogs = ['Roger', 'Syd']
</script>

{#each goodDogs as goodDog}
	<li>{goodDog}</li>
{/each}

If you are familiar with other frameworks that use templates, it’s a very similar syntax.

如果您熟悉使用模板的其他框架,则其语法非常相似。

You can get the index of the iteration using:

您可以使用以下方法获取迭代的索引:

<script>
let goodDogs = ['Roger', 'Syd']
</script>

{#each goodDogs as goodDog, index}
	<li>{index}: {goodDog}</li>
{/each}

(indexes start at 0)

(索引从0开始)

When dynamically editing the lists removing and adding elements, you should always pass an identifier in lists, to prevent issues.

动态编辑列表中的删除和添加元素时,应始终在列表中传递标识符,以防止出现问题。

You do so using this syntax:

您可以使用以下语法:

<script>
let goodDogs = ['Roger', 'Syd']
</script>

{#each goodDogs as goodDog (goodDog)}
	<li>{goodDog}</li>
{/each}

<!-- with the index -->
{#each goodDogs as goodDog, index (goodDog)}
	<li>{goodDog}</li>
{/each}

You can pass an object, too, but if your list has a unique identifier for each element, it’s best to use it:

您也可以传递一个对象,但是如果列表中每个元素都有一个唯一的标识符,则最好使用它:

<script>
let goodDogs = [
  { id: 1, name: 'Roger'},
  { id: 2, name: 'Syd'}
]
</script>

{#each goodDogs as goodDog (goodDog.id)}
	<li>{goodDog.name}</li>
{/each}

<!-- with the index -->
{#each goodDogs as goodDog, index (goodDog.id)}
	<li>{goodDog.name}</li>
{/each}

翻译自: https://flaviocopes.com/svelte-templates-loops/

xsl 模板 循环

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值