Introduction
Slots let you put custom content inside a component. This helps make components flexible and reusable.
Slots let you put custom content inside a component. This helps make components flexible and reusable.
<template> <slot></slot> </template>
<template> <slot></slot> </template>
<template>
<slot>Default content if none provided</slot>
</template><template> <slot name="header"></slot> <slot></slot> <!-- default slot --> <slot name="footer"></slot> </template>
<MyComponent>
<template #header>
<h1>Title here</h1>
</template>
<p>Main content goes here.</p>
<template #footer>
<button>Close</button>
</template>
</MyComponent>This example shows a Card component with three slots: header, default body, and footer.
The parent App.vue uses named slots to put a title in the header, some text in the body, and a button in the footer.
If any slot content was missing, the card would show its default content instead.
<!-- Card.vue --> <template> <div class="card"> <header class="card-header"> <slot name="header">Default Header</slot> </header> <section class="card-body"> <slot>Default body content</slot> </section> <footer class="card-footer"> <slot name="footer">Default Footer</slot> </footer> </div> </template> <style scoped> .card { border: 1px solid #ccc; padding: 1rem; border-radius: 0.5rem; max-width: 300px; } .card-header, .card-footer { background-color: #f0f0f0; padding: 0.5rem; font-weight: bold; } .card-body { margin: 1rem 0; } </style> <!-- App.vue --> <template> <Card> <template #header> <h2>Welcome!</h2> </template> <p>This is the main content inside the card.</p> <template #footer> <button @click="alert('Clicked!')">Click me</button> </template> </Card> </template> <script setup> import Card from './Card.vue' </script>
header in a Vue component?<slot name="header"></slot> matches the correct Vue syntax for named slots.<ChildComponent> <template #header>Header Content</template> Default Content </ChildComponent>
<div> <header><slot name="header">Default Header</slot></header> <main><slot>Default Content</slot></main> </div>
<header> and <main> tags?<div> <slot name="footer">Default Footer</slot> <slot>Default Content</slot> </div>
<ChildComponent> <template #foot>Custom Footer</template> </ChildComponent>
Card component with three slots: header, body, and footer. The body slot should have default text "No content provided" if empty. Which child component template correctly implements this?<div class="card"> <header><slot name="header">No header</slot></header> <section><slot name="body">No content provided</slot></section> <footer><slot name="footer">No footer</slot></footer> </div>correctly names all slots and provides default content for all, including the body slot with "No content provided".
<div class="card"> <header><slot name="header">No header</slot></header> <section><slot name="body">No content provided</slot></section> <footer><slot name="footer">No footer</slot></footer> </div>[OK]
Pass the test and this pattern moves unknown