Vue Slots
In some scenarios, we may need to pass template content from a parent component to a child component. For example, a generic card component where we want its main content to be customizable. In such cases, we need to use Slots.
Slots are a powerful feature of Vue components that allow you to compose components in a predefined way, "inserting" content from the parent component into specific locations in the child component.
Default Slot
The simplest form of slots is a single, default slot. The child component can use the <slot> tag to create a "content outlet" for itself.
FancyButton.vue (Child Component)
When the parent component uses FancyButton, it can pass any template content inside it. This content will be rendered at the location of the <slot> tag in the child component.
App.vue (Parent Component)
The rendered HTML will be:
Named Slots
Sometimes we need multiple slots. For example, a layout component with a header, main content, and footer. For this, the <slot> element has a special attribute name that can be used to assign unique IDs to different slots.
BaseLayout.vue (Child Component)
To provide content to named slots in the parent component, we need to use a <template> element with the v-slot directive, passing the slot name as the parameter to v-slot.
App.vue (Parent Component)
v-slot has a dedicated shorthand syntax #, so <template v-slot:header> can be abbreviated to <template #header>.
Scoped Slots
Sometimes, it's useful to allow slot content to access data that only exists in the child component. For example, a list component where we want the parent component to customize the rendering of each item.
To achieve this, we can pass attributes to the slot, similar to how we do with props.
MyComponent.vue (Child Component)
Now, in the parent scope, we can use v-slot with a value to define the name of the slot props we provide.
App.vue (Parent Component)
In this example, we chose to name the object containing all slot props slotProps, but you can use any name you like. You can also use destructuring to get specific props:
Scoped slots allow us to create highly flexible and reusable components, encapsulating data logic in the child component while giving the parent component control over the visual presentation.