<script>
let count=0;
</script>
{#each list as item, i}
<div class="item">
<Item />
</div>
{/each}
如何在 svelte 的每个 block 内增加某些条件下的计数?
最佳答案
如果你需要增加实际的count
变量,您可以在 <script>
中定义的回调中执行此操作标签:
<script>
let count = 0;
const increment = () => count++;
</script>
{#each list as item, i}
<div class="item">
<!-- Use increment() somewhere in here -->
<Item />
</div>
{/each}
如果您需要做的就是将一些金额添加到 count
在不改变原始变量的情况下,您可以在大括号内这样说:
<script>
let count = 0;
</script>
{#each list as item, i}
<div class="item">
{count + i}
<Item />
</div>
{/each}
关于 slim : change variable value inside html block,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68393239/