就像在 JavaScript 中一樣,if
區塊可以有一個 else
區塊
App
{#if count > 10}
<p>{count} is greater than 10</p>
{:else}
<p>{count} is between 0 and 10</p>
{/if}
{#...}
開啟一個區塊。{/...}
關閉一個區塊。{:...}
*繼續*一個區塊。恭喜 — 您已經學會 Svelte 添加到 HTML 的幾乎所有語法。
上一個 下一個
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<script>
let count = $state(0);
function increment() {
count += 1;
}
</script>
<button onclick={increment}>
Clicked {count}
{count === 1 ? 'time' : 'times'}
</button>
{#if count > 10}
<p>{count} is greater than 10</p>
{/if}