N
Nuxt2mo ago
toni

Elegant way to pass slots to children

Is there a more elegant way to do this?
<LayoutHeader>
<template #navitems>
<slot name="navitems" />
</template>
</LayoutHeader>
<LayoutHeader>
<template #navitems>
<slot name="navitems" />
</template>
</LayoutHeader>
If I do smth like
<LayoutHeader v-slot:navitems="$slots.navitems" />
<LayoutHeader v-slot:navitems="$slots.navitems" />
there are SSR errors...
2 Replies
kapa.ai
kapa.ai2mo ago
Hi @toni. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The most reliable and idiomatic way to forward named slots from a parent to a child component in Nuxt (and Vue) is the pattern you already use:
<LayoutHeader>
<template #navitems>
<slot name="navitems" />
</template>
</LayoutHeader>
<LayoutHeader>
<template #navitems>
<slot name="navitems" />
</template>
</LayoutHeader>
This approach is explicit and works consistently with SSR, as it ensures the slot content is rendered in the correct context. The alternative you mentioned:
<LayoutHeader v-slot:navitems="$slots.navitems" />
<LayoutHeader v-slot:navitems="$slots.navitems" />
is not supported and will cause errors, especially in SSR, because Vue's slot syntax does not allow passing slot functions directly like this. The slot content must be rendered within the parent's template context. There is no more "elegant" or concise way documented in the official Nuxt or Vue docs for this use case. The explicit slot forwarding pattern is the recommended and robust solution. If you are using Nuxt Content and want to forward slots, you can use the native <slot> component, and for named slots, you can use the name prop as shown in the Nuxt Content slot documentation. But the pattern remains the same: you must explicitly forward each slot you want to pass down. In summary:
Stick with your current approach—it's the correct and reliable way to forward named slots in Nuxt and Vue. There is no documented, more concise alternative that avoids SSR issues or is more "elegant" in the official sources. If you have many slots to forward, you might consider a render function or a utility component, but this is not covered in the official documentation and may introduce complexity or SSR issues. So, explicit forwarding is the best practice according to the available knowledge sources. Nuxt Content slot documentation ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@388707874718613506> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?