Performance: Slots for content distribution
Slots affect how Vue distributes and renders child content inside components, impacting rendering speed and layout stability.
<MyComponent> <template v-slot:default> <ul> <li v-for="item in items" :key="item.id"> {{ item.text }} </li> </ul> </template> </MyComponent>
<MyComponent> <template v-slot:default> <div v-for="item in items" :key="item.id"> {{ item.text }} </div> </template> </MyComponent>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Unkeyed dynamic slot content with many elements | High (many nodes) | Multiple reflows per update | High paint cost | [X] Bad |
| Keyed slot content wrapped in semantic elements | Moderate (structured nodes) | Single reflow per update | Moderate paint cost | [OK] Good |
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