$$slots
在符文模式下,我們知道哪些 片段 被提供給元件,因為它們只是正常的 props。
在傳統模式下,要知道是否為給定的插槽提供了內容的方式是使用 $$slots
物件,該物件的鍵是由父元件傳遞到元件中的插槽名稱。
卡片
<div>
<slot name="title" />
{#if $$slots.description}
<!-- This <hr> and slot will render only if `slot="description"` is provided. -->
<hr />
<slot name="description" />
{/if}
</div>
應用程式
<Card>
<h1 slot="title">Blog Post Title</h1>
<!-- No slot named "description" was provided so the optional slot will not be rendered. -->
</Card>
上一個 下一個