Performance: Named slots and scoped slots
This concept affects rendering performance by controlling how much DOM is created and how often components re-render due to slot content changes.
<ChildComponent> <template #default="{ data }"> <div>{{ data }}</div> </template> </ChildComponent>
<ChildComponent>
<template #default>
<div>{{ expensiveComputation() }}</div>
</template>
</ChildComponent>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Inline dynamic slot content | High (many nodes updated) | Multiple reflows per update | High paint cost due to frequent changes | [X] Bad |
| Scoped slots with precomputed props | Low (minimal updates) | Single reflow per update | Low paint cost due to fewer changes | [OK] Good |
| Named slots with inline static content | Medium (recreated each render) | Some reflows due to patching | Medium paint cost | [!] OK |
| Named slots with static child components | Low (cached nodes) | Minimal reflows | Low paint cost | [OK] Good |
header inside a Vue component?<MyList>
<template #item="{ item }">
<li>{{ item.text }}</li>
</template>
</MyList><ul> <slot name="item" :item="listItem"></slot> </ul>
listItem is { text: 'Hello' }?listItem as item to the slot content using :item="listItem".item.textitem and renders <li>{{ item.text }}</li>, which becomes <li>Hello</li>.<MyComponent> <template slot="footer">Footer Content</template> </MyComponent>
MyComponent defines a named slot footer using <slot name="footer">.slot attribute on template is deprecated in Vue 3.v-slot:footer or shorthand #footer instead of slot="footer".slot attribute instead of #footer shorthand -> Option BCard component with named slots for header, body, and footer. The body slot should receive a scoped prop info from the component. Which is the correct way to define the Card template?body slot must receive the scoped prop info from the component's data.body slot has :info='cardInfo' to pass the scoped prop. The others are simple named slots without props.Pass the test and this pattern moves unknown