Vue v-slot
To refer to named slots, we require the v-slot directive.
More control over the content’s placement within the child component’s template is possible using named slots.
Components that are more reusable and adaptable can be made with named slots.
Let’s examine what occurs if we utilize two slots in the component before using v-slot and named slots:
Example
App.vue:
App.vue
The component has two div tags with one slot in each.
'Hello!'
SlotComp.vue:
Component
We can observe that when a component has two slots, the material just shows up twice.
v-slot and Named Slots
If a component has many <slot> and we wish to specify which <slot> the content appears in, we must use v-slot to transport the content to the appropriate location and name the slots.
Example
We give the slots distinct names so that we can distinguish between them.
SlotComp.vue:
Component
And now we can direct the content to the appropriate slot using App.vue is v-slot feature.
App.vue:
App.vue
The component has two div tags with one slot in each.
'Hello!'
Default Slots
If you have an empty <slot>, it will be the default for any components that are not indicated with v-slot or that are marked with v-slot:default.
We only need to make two little adjustments to our prior example in order to see how this functions:
Example
SlotComp.vue:
Component
App.vue:
App.vue
The component has two div tags with one slot in each.
'Hello!'
To further emphasize that the content belongs in the default slot, we can label it with the default value v-slot:default, as was previously described.
Example
SlotComp.vue:
Component
App.vue:
App.vue
The component has two div tags with one slot in each.
'Default slot'
v-slot in < template >
The v-slot directive can be used as an attribute in the component tag, as you have seen.
Moreover, v-slot can be used in a <template> element to target certain <slot> for longer sections of material.
Example
App.vue:
App.vue
The component has two div tags with one slot in each.
To the bottom slot!
This p tag and the h4 tag above are directed to the bottom slot with the v-slot directive used on the template tag.
This goes into the default slot
SlotComp.vue:
Component
Since the <template> element is merely a placeholder for the content and is not rendered, we use it to direct certain content to a specific <slot>. Examining the produced page will show you this because the template tag is absent.
v-slot Shorthand #
# is the abbreviation for v-slot:.
This implies that:
'Hello!'
Writable as:
'Hello!'
Example
App.vue:
App.vue
The component has two div tags with one slot in each.
'Hello!'
SlotComp.vue:
Component