In this article, we will fully understand the vue slots through the practical application of its various use cases. Let’s start with knowing about vue slots first.
What are Vue Slots?
Slots are reserved spaces offered by VueJs to display content passed down from one component to another. There are two types of slots in vuejs namely: named slot and unnamed(default) slot.
Practical Use Case of Vue Slots
- To pass down HTML elements from one component to another.
With props, Vue allows us to pass strings, objects, arrays, and functions from a parent component to its child component. While we can pass HTML elements from a parent to its child component as a string this will make our website vulnerable to cross-site scripting attacks that is why VueJs provides us with a slot that is a more secure and reliable method of passing HTML elements and other contents from parent to its child component for rendering.
HOW TO USE SLOT In the child component where the content is to be displayed, add the slot tag as follows:
<slot></slot>
In this tutorial, we will generate our project with the Vue CLI
vue create slot-project
In the src folder create a components folder with parent.vue and child.vue files
Adding the code below to child.vue
<template>
<div class="container">
<div class="row">
<div class="col-xs-12">
<h2>Child Component</h2>
</div>
</div>
</div>
</template>
<script>
export default {
}
</script>
<style>
</style>
Add the code snippet below to parent.vue
<template>
<div class="container">
<div class="row">
<div class="col-xs-12">
<h2>Parent</h2>
<child-component>
<h3 class="child-content">You are my first child</h3>
<h4 class="child-content">Your name is Tom</h4>
</child-component>
</div>
</div>
</div>
</template>
<script>
import child from './child.vue'
export default {
components: {
child-component: child
}
}
</script>
<style>
</style>
Add the code snippet below to parent.vue
<template>
<div class="container">
<div class="row">
<div class="col-xs-12">
<h2>Parent</h2>
<child-component>
<h3 class="child-content">You are my first child</h3>
<h4 class="child-content">Your name is Tom</h4>
</child-component>
</div>
</div>
</div>
</template>
<script>
import child from './child.vue'
export default {
components: {
child-component: child
}
}
</script>
<style>
</style>
Here we imported the child component and passed down the HTML content to the child.
For these contents to be displayed in the child component, the slot tag must be added to the child component.
Let’s add the slot tag to the child.vue file as follows:
<template>
<div class="container">
<div class="row">
<div class="col-xs-12">
<h2>Child Component</h2>
<slot></slot>
</div>
</div>
</div>
</template>
<script>
export default {
}
</script>
<style>
</style>
In the app.js file add the parent.vue component
<template>
<div class="app">
<Parent/>
</div>
</template>
<script>
import Parent from './components/Parent'
export default {
components: {
Parent
}
}
</script>
<style>
</style>
Now, we can verify that the slot is working as expected.
npm run serve
Now our app should be like this:
STYLING SLOT COMPONENT
For styling our slot component, the CSS styles should be added with the slot tag.
So in child.vue component we will add the following styles to the HTML content received from the parent.vue component.
<style>
.child-content {
background-color: blue;
color: red;
}
</style>
Using Multiple Slots
To use multiple slots in vue, vuejs provides us with a way to name our slots.
What if we want the h2 and h3 tags in the parent component to be displayed individually in separate slots. This would be a typical use case for named slots.
In the Parent.vue component we will name our slots as follows:
<template>
<div class="container">
<div class="row">
<div class="col-xs-12">
<h2>Parent</h2>
<child-component>
<h3 class="child-content" slot="message">You are my first child</h3>
<h4 class="child-content" slot="name">Your name is Tom</h4>
</child-component>
</div>
</div>
</div>
</template>
In the child.vue component we will receive the named slot as follows:
<template>
<div class="container">
<div class="row">
<div class="col-xs-12">
<h2>Child Component</h2>
<slot name="message"></slot>
<slot name="name"></slot>
</div>
</div>
</div>
</template>
Here vuejs reserves two slots to display the content of the slot attribute with the value of message and name as separate contents.
Vue Templates
If you are looking for VueJs Admin Dashboard Template then you can check out below useful Admin Template which can save you time, money, and energy:
Modernize Free Vuetify + Vue js Admin Dashboard

Conclusion
In this article, we have seen a practical use case of vue slots to transfer content from a parent component to a child component.
Related Article: The Ultimate Vue Cheat Sheet | Vue 3 & 2
Hello there, just became aware of your blog through Google, and found that it is
truly informative. I’m gonna watch out for brussels.
I will be grateful if you continue this in future.
Many people will be benefited from your writing. Cheers!
Heya! I’m at work browsing your blog from my new iphone 3gs!
Just wanted to say I love reading through your blog and look forward to all
your posts! Keep up the superb work!
Wow, fantastic blog layout! How long have you ever been blogging for?
you made blogging look easy. The total glance of your
web site is great, as smartly as the content!
I know this website gives quality based articles or reviews and extra
information, is there any other web site which offers these
kinds of data in quality?
Hello, I desire to subscribe for this website to get most up-to-date updates, so where can i do it please
assist.
Howdy just wanted to give you a brief heads up and let you
know a few of the pictures aren’t loading properly. I’m not sure why but I
think its a linking issue. I’ve tried it in two different browsers and both show the same outcome.
I like it when folks get together and share opinions.
Great site, continue the good work!
Somebody essentially help to make severely posts
I’d state. That is the first time I frequented your website page
and up to now? I surprised with the analysis
you made to create this particular post incredible.
Excellent activity!
I needed to thank you for this fantastic read!! I certainly enjoyed every little bit of it.
I’ve got you book marked to check out new things you post…
Hello just wanted to give you a brief heads up and let
you know a few of the pictures aren’t loading correctly.
I’m not sure why but I think its a linking issue. I’ve tried it in two different internet browsers and both show the same
results.
It’s perfect time to make some plans for the future and
it is time to be happy. I have read this post and if I could I
wish to suggest you few interesting things or suggestions.
Perhaps you can write next articles referring to this article.
I want to read more things about it!
Thank you for sharing your thoughts. I really appreciate your efforts and I will be waiting for your further write ups thanks once
again.
I am really loving the theme/design of your blog.
Do you ever run into any web browser compatibility issues?
A couple of my blog readers have complained
about my website not operating correctly in Explorer but looks great in Safari.
Do you have any solutions to help fix this issue?
Very nice post. I just stumbled upon your
blog and wished to say that I’ve really enjoyed browsing your
blog posts. In any case I will be subscribing to your feed
and I hope you write again very soon!
I could not refrain from commenting. Very well
written!
Wow! At last I got a webpage from where I be able to really take
valuable information regarding my study and knowledge.
Excellent beat ! I wish to apprentice even as you amend your website, how could i subscribe
for a blog site? The account helped me a applicable deal.
I were tiny bit acquainted of this your broadcast offered vibrant transparent idea
This text is priceless. When can I find out more?
I am regular visitor, how are you everybody? This post posted at this website
is really nice.
It’s in point of fact a great and useful piece
of info. I’m happy that you simply shared this useful info
with us. Please keep us informed like this. Thank
you for sharing.
It’s a pity you don’t have a donate button! I’d definitely donate to this fantastic blog!
I guess for now i’ll settle for book-marking and adding your RSS
feed to my Google account. I look forward to brand new
updates and will share this site with my Facebook group.
Talk soon!
I got this web site from my friend who shared with me concerning this website
and now this time I am browsing this web site and reading very informative articles or reviews
at this place.
Hiya! I know this is kinda off topic however I’d figured I’d ask.
Would you be interested in exchanging links or maybe
guest writing a blog post or vice-versa? My site discusses a lot
of the same subjects as yours and I believe we could
greatly benefit from each other. If you might be interested
feel free to shoot me an email. I look forward to hearing from you!
Awesome blog by the way!
It’s the best time to make some plans for the future and it’s time
to be happy. I have read this post and if I could I want to
suggest you some interesting things or suggestions. Maybe you
could write next articles referring to this article. I wish to read more things about
it!