Skip to main content



{#if ...}

{#if expression}...{/if}
{#if expression}...{:else if expression}...{/if}
{#if expression}...{:else}...{/if}

有条件渲染的内容可以封装在 if 块中。

Content that is conditionally rendered can be wrapped in an if block.

{#if answer === 42}
	<p>what was the question?</p>

可以使用 {:else if expression} 添加附加条件,也可以选择以 {:else} 子句结尾。

Additional conditions can be added with {:else if expression}, optionally ending in an {:else} clause.

{#if porridge.temperature > 100}
	<p>too hot!</p>
{:else if 80 > porridge.temperature}
	<p>too cold!</p>
	<p>just right!</p>


(Blocks don't have to wrap elements, they can also wrap text within elements!)

{#each ...}

{#each expression as name}...{/each}
{#each expression as name, index}...{/each}
{#each expression as name (key)}...{/each}
{#each expression as name, index (key)}...{/each}
{#each expression as name}...{:else}...{/each}

可以使用 each 块来迭代值列表。

Iterating over lists of values can be done with an each block.

<h1>Shopping list</h1>
	{#each items as item}
		<li>{} x {item.qty}</li>

你可以使用每个块来迭代任何数组或类似数组的值 — 即任何具有 length 属性的对象。

You can use each blocks to iterate over any array or array-like value — that is, any object with a length property.

Each 块还可以指定一个索引,相当于 回调中的第二个参数:

An each block can also specify an index, equivalent to the second argument in an callback:

{#each items as item, i}
	<li>{i + 1}: {} x {item.qty}</li>

如果提供了关键表达式 — 必须唯一标识每个列表项 — 当数据更改时,Svelte 将使用它来比较列表,而不是在末尾添加或删除项目。 键可以是任何对象,但建议使用字符串和数字,因为它们允许在对象本身更改时保留身份。

If a key expression is provided — which must uniquely identify each list item — Svelte will use it to diff the list when data changes, rather than adding or removing items at the end. The key can be any object, but strings and numbers are recommended since they allow identity to persist when the objects themselves change.

{#each items as item (}
	<li>{} x {item.qty}</li>

<!-- or with additional index value -->
{#each items as item, i (}
	<li>{i + 1}: {} x {item.qty}</li>


You can freely use destructuring and rest patterns in each blocks.

{#each items as { id, name, qty }, i (id)}
	<li>{i + 1}: {name} x {qty}</li>

{#each objects as { id, }}
	<li><span>{id}</span><MyComponent {} /></li>

{#each items as [id,]}
	<li><span>{id}</span><MyComponent values={rest} /></li>

Each 块还可以有一个 {:else} 子句,如果列表为空,则渲染该子句。

An each block can also have an {:else} clause, which is rendered if the list is empty.

{#each todos as todo}
	<p>No tasks today!</p>

从 Svelte 4 开始,可以迭代像 MapSet 这样的可迭代对象。 可迭代对象必须是有限的和静态的(它们在迭代时不应改变)。 在底层,它们在传递到渲染之前使用 Array.from 转换为数组。 如果你正在编写对性能敏感的代码,请尝试避免迭代并使用常规数组,因为它们的性能更高。

Since Svelte 4 it is possible to iterate over iterables like Map or Set. Iterables need to be finite and static (they shouldn't change while being iterated over). Under the hood, they are transformed to an array using Array.from before being passed off to rendering. If you're writing performance-sensitive code, try to avoid iterables and use regular arrays as they are more performant.

{#await ...}

{#await expression}...{:then name}...{:catch name}...{/await}
{#await expression}...{:then name}...{/await}
{#await expression then name}...{/await}
{#await expression catch name}...{/await}

Await 块允许你在 Promise 的三种可能状态上进行分支 — 待定、已完成或已拒绝。 在 SSR 模式下,只有待处理的分支才会在服务器上渲染。 如果提供的表达式不是 Promise,则只会呈现已完成的分支,包括在 SSR 模式下。

Await blocks allow you to branch on the three possible states of a Promise — pending, fulfilled or rejected. In SSR mode, only the pending branch will be rendered on the server. If the provided expression is not a Promise only the fulfilled branch will be rendered, including in SSR mode.

{#await promise}
	<!-- promise is pending -->
	<p>waiting for the promise to resolve...</p>
{:then value}
	<!-- promise was fulfilled or not a Promise -->
	<p>The value is {value}</p>
{:catch error}
	<!-- promise was rejected -->
	<p>Something went wrong: {error.message}</p>

如果你在 Promise 拒绝时不需要渲染任何内容(或者不可能出现错误),则可以省略 catch 块。

The catch block can be omitted if you don't need to render anything when the promise rejects (or no error is possible).

{#await promise}
	<!-- promise is pending -->
	<p>waiting for the promise to resolve...</p>
{:then value}
	<!-- promise was fulfilled -->
	<p>The value is {value}</p>


If you don't care about the pending state, you can also omit the initial block.

{#await promise then value}
	<p>The value is {value}</p>

同样,如果只想显示错误状态,则可以省略 then 块。

Similarly, if you only want to show the error state, you can omit the then block.

{#await promise catch error}
	<p>The error is {error}</p>

{#key ...}

{#key expression}...{/key}


Key blocks destroy and recreate their contents when the value of an expression changes.


This is useful if you want an element to play its transition whenever a value changes.

{#key value}
	<div transition:fade>{value}</div>


When used around components, this will cause them to be reinstantiated and reinitialised.

{#key value}
	<Component />
上一页 基本标记
下一页 特殊标签