Skip to main content

就像 JavaScript 一样,if 代码块中可以有 else 代码块:

App.svelte
{#if count > 10}
	<p>{count} is greater than 10</p>
{:else}
	<p>{count} is between 0 and 10</p>
{/if}

# 符号永远标志着 一个代码块的开始/ 符号永远标志着 一个代码块的结束: 符号,就比如 {:else} 中的这个,标志着 代码块的继续。不用担心,目前为止你已经学完了所有 Svelte 添加到 HTML 中的语法了。

Next: Else-if 代码块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<script>
	let count = 0;
 
	function increment() {
		count += 1;
	}
</script>
 
<button on:click={increment}>
	Clicked {count}
	{count === 1 ? 'time' : 'times'}
</button>
 
{#if count > 10}
	<p>{count} is greater than 10</p>
{/if}
initialising