如何安装svelte_如何在Svelte模板中模拟for循环

本文介绍了如何在Svelte模板中使用each块进行迭代,以及当需要基于变量重复执行代码块时,如何通过创建特定长度的数组来模拟for循环。
摘要由CSDN通过智能技术生成

如何安装svelte

Svelte templates offer us the fantastic each block that lets us iterate on an array, or anything that is iterable:

苗条的模板为我们提供了奇妙的每个块,使我们可以在数组或任何可迭代的对象上进行迭代:

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

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

But what if you want to repeat a block for a few times, based on a variable? Let’s say we have the rows variable that holds a number, and we want to use that as the loop variable.

但是,如果您想基于变量重复执行几次块,该怎么办? 假设我们有保存数字的rows变量,并且我们想将其用作循环变量。

We can do what we need by creating an array, using the Array(n) syntax. This will create an array, initializing it with n items:

我们可以通过使用Array(n)语法创建一个数组来完成所需的工作。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值