Template Slot Vue Js
Slots are reserved space offered by vuejs to display content passed down from one component to another. There are two types of the slot in vuejs namely: named slot and unnamed (default) slot. Looking for Vue Templates? Try our Vue Templates and create stunning web applications for unlimited client projects and personal projects. Practical use of Components and Mixins in Vue JS; 2.6.0+ If you use Vue version above 2.6.0, Vue introduces new unified slot api, which is v-slot. It replaces the slot and slot-scope attributes, which are deprecated, but have not been removed and are still documented here. You can refer to deprecated API here. # Slots (Default).
A component can be 100% responsible for generating its output, like in this case:
Podcast: Play in new window Download In this episode of Views on Vue, Lindsay and Raymond talk with Michael Thiessen about his new course, Reusable Components. We discuss Michael’s six levels of reusability, and his process in building the course to help developers have their own ‘aha’ moments with these concepts.
or it can also let the parent component inject any kind of content into it, using slots.
What is a slot? It’s a space in your component output that is reserved, waiting to be filled.
You define a slot by putting <slot></slot>
in a component template:
When using this component, any content added between the opening and closing tag will be added inside the slot placeholder:
If you put any content side the <slot></slot>
tags, that serves as the default content in case nothing is passed in.
A complicated component layout might require a better way to organize content, with multiple slots as well.
This is why Vue offers us named slots.
Named slots
With a named slot you can assign parts of a slot to a specific position in your component template layout, and you use a slot
attribute to any tag, to assign content to that slot.
Anything outside any template tag is added to the main slot
.
For convenience I use a page
single file component in this example:
Here is how we can use it, providing the slots content, in a parent component:
There is a handy shorthand, #
:
Note: Vue 2.6 deprecated the slot
attribute in favor of v-slot
, and requires it to be added to a template
tag (while slot
could be applied to any tag)
Scoped slots
In a slot, we can’t access the data contained in the child component from the parent.
Vue recognizes this use case and provides us a way to do so:
In the parent we can access the dog name we passed using:
slotProps
is just a variable we used to access the props we passed. You can also avoid setting a variable just to hold the props you pass to the child component, by destructuring the object on the fly:
More vue tutorials:
In this tutorial, we will learn about how to use the slots in vue.js with the help of examples.
What are Slots?
Slots helps us to pass the data between opening and closing component tags.
In vue.js props are used to pass the data to its child components, but it is hard to pass when we have a complex code. In such cases slots can be used.
Let’s create a new component called Post
by adding the <slot>
element.
Now, if we pass any content between the Post
component opening and closing tags that are rendered in the place of <slot></slot>
element.
Output:
Named Slots
Sometimes, we need to pass the data to a specific places in such cases named slots can be used.
The named slots can be created by adding a name
attribute to the <slot>
element.
To pass the content to the named slots we need to use v-slot
directive on template providing slot name as v-slot
argument.
Fallback data
In some cases, we can use fallback data (aka default) when data is not passed to a slot.
For example:
Template Slot Vue Js Free
In the above component, we have added a Submit
text inside a slot
element.
Template Slot Vue Js Download
Now, If we use a my-button
component without passing any data we can seethe fallback data Submit
text is rendered inside the button.
Output of rendered html:
Template Slot Vue Json
But, if we pass data to the my-button
component fallback data is replaced.
Output of rendered html: