Oftentimes you will need to allow your parent Vue components to embed arbitrary content inside of child components. Vue provides a way to accomplish this with slots.
Note: If you are coming from an Angular background, this is a similar concept to transclusion or content projection.
In this tutorial, you will explore an example Vue project with a parent component and child component that shares content with slots.
If you would like to follow along with this article, you will need:
This tutorial was verified with Node v15.10.0, npm
v7.6.0, and vue
v2.6.11.
To allow a parent component to pass DOM elements into a child component, provide a <slot>
element inside the child component.
Here is an example of a ChildComponent
that contains a <slot>
:
Here is an example of a ParentComponent
that populates the ChildComponent
with content:
Viewing the application in a web browser will produce the following result:
Content and data from the parent component are injected into the child component.
If the parent component does not inject any content into the child component’s <slot>
, the child component will render any elements inside its <slot>
tag:
Here is an example of a ChildComponent
with fallback content:
Here is an example of a ParentComponent
that has two ChildComponent
s - one with slot content and one without:
Viewing the application in a web browser will produce the following result:
Fallback content appears when slot content is not provided by the parent component.
Note: If there is no <slot>
element in the child component <template>
, any content from the parent component will be silently discarded.
This completes a brief introduction into using <slot>
and fallbacks.
Having one <slot>
element to inject content can satisfy some use cases. However, you may have other use cases where you want to utilize multiple <slot>
elements. It is possible to achieve this with Vue with named slots.
Named slots are <slot>
elements with a name attribute to allow for namespaced content injection:
Consider an example component that features named slots for main
and aside
:
The parent component populates the main
and aside
slots. Content that does not reference a named slot populates the empty slot:
Note: Prior to Vue 2.6.0, you would have used slot
attributes. It has since been replaced with the v-slot
directive. Vue 3 will deprecate and eventually remove slot
attributes.
Viewing the application in a web browser will produce the following result:
This completes a brief introduction into using named slots.
In this tutorial, you explored a Vue project with a parent component and child component example that shares content with slots.
If you’d like to learn more about Vue.js, check out our Vue.js topic page for exercises and programming projects.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!