Skip to main content

{@render ...}

要渲染 snippet,请使用 {@render ...} 标签。

¥To render a snippet, use a {@render ...} tag.

{#snippet sum(a, b)}
	<p>{a} + {b} = {a + b}</p>
{/snippet}

{@render sum(1, 2)}
{@render sum(3, 4)}
{@render sum(5, 6)}

表达式可以是像 sum 这样的标识符,也可以是任意的 JavaScript 表达式:

¥The expression can be an identifier like sum, or an arbitrary JavaScript expression:

{@render (cool ? coolSnippet : lameSnippet)()}

可选片段(Optional snippets)

¥Optional snippets

如果代码片段可能未定义(例如,因为它是一个传入的 prop),那么你可以使用可选链接仅在定义时渲染它:

¥If the snippet is potentially undefined — for example, because it’s an incoming prop — then you can use optional chaining to only render it when it is defined:

{@render children?.()}

或者,使用带有 :else 子句的 {#if ...} 块来渲染后备内容:

¥Alternatively, use an {#if ...} block with an :else clause to render fallback content:

{#if children}
	{@render children()}
{:else}
	<p>fallback content</p>
{/if}
上一页 下一页