在构建用户界面的时候,你时常需要处理列表数据。在这个练习中,我们需要重复编写多个 button
标签,每次修改不同的颜色,但万一还有更多需要添加怎么办。
摒弃这种费力地拷贝、粘贴、修改,我们可以只用一个 button
外加一个 each
代码块:
App.svelte
<div>
{#each colors as color}
<button
aria-current={selected === 'red'}
aria-label="red"
style="background: red"
on:click={() => selected = 'red'}
></button>
{/each}
</div>
表达式的部分(此例中的
colors
)可以换成任何数组或者类似数组的对象(即有length
属性)。你也可以直接用通用的可迭代对象:each [...iterable]
。
现在我们需要用 color
变量替换掉 "red"
:
App.svelte
<div>
{#each colors as color}
<button
aria-current={selected === color}
aria-label={color}
style="background: {color}"
on:click={() => selected = color}
></button>
{/each}
</div>
你也可以通过第二个参数获取列表的当前 索引,比如:
App.svelte
<div>
{#each colors as color, i}
<button
aria-current={selected === color}
aria-label={color}
style="background: {color}"
on:click={() => selected = color}
>{i + 1}</button>
{/each}
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<script>
const colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'];
let selected = colors[0];
</script>
<h1 style="color: {selected}">Pick a colour</h1>
<div>
<button
aria-current={selected === 'red'}
aria-label="red"
style="background: red"
on:click={() => selected = 'red'}
></button>
<button
aria-current={selected === 'orange'}
aria-label="orange"
style="background: orange"
on:click={() => selected = 'orange'}
></button>
<button
aria-current={selected === 'yellow'}
aria-label="yellow"
style="background: yellow"
on:click={() => selected = 'yellow'}
></button>
<!-- TODO add the rest of the colours -->
<button></button>
<button></button>
<button></button>
<button></button>
</div>
<style>
h1 {
transition: color 0.2s;
}
div {
display: grid;
grid-template-columns: repeat(7, 1fr);
grid-gap: 5px;
max-width: 400px;
}
button {
aspect-ratio: 1;
border-radius: 50%;
background: var(--color, #fff);
transform: translate(-2px,-2px);
filter: drop-shadow(2px 2px 3px rgba(0,0,0,0.2));
transition: all 0.1s;
}
button[aria-current="true"] {
transform: none;
filter: none;
box-shadow: inset 3px 3px 4px rgba(0,0,0,0.2);
}
</style>