How to use the slots in Vue.js

How to use the slots in Vue.js

In this post we will give you information about How to use the slots in Vue.js. Hear we will give you detail about How to use the slots in Vue.jsAnd how to use it also give you demo for it if it is necessary.

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.

Post.vue
&lt;template&gt; &lt;div&gt;   &lt;slot&gt;&lt;/slot&gt; &lt;/div&gt;&lt;/template&gt;&lt;script&gt;export default{}&lt;/script&gt;

Now, if we pass any content between the Post component opening and closing tags that are rendered in the place of <slot></slot> element.

App.vue
&lt;template&gt;  &lt;div&gt;    &lt;Post&gt;      &lt;h1&gt;What is Lorem Ipsum?&lt;/h1&gt;      &lt;p&gt;        What is Lorem Ipsum?Lorem Ipsum is simply dummy text of the printing        andtypesetting industry.Lorem Ipsum has been the industry's standard        dummy.      &lt;/p&gt;    &lt;/Post&gt;  &lt;/div&gt;&lt;/template&gt;&lt;script&gt;import Post from "./components/Post";export default {  components: {    Post  }};&lt;/script&gt;

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.

Post.vue
&lt;template&gt; &lt;div&gt;   <span>&lt;slot name="title"&gt;&lt;/slot&gt;   &lt;slot name="description"&gt;&lt;/slot&gt; &lt;/div&gt;&lt;/template&gt;&lt;script&gt;export default{}&lt;/script&gt;

To pass the content to the named slots we need to use v-slot directive on template providing slot name as v-slot argument.

App.vue
&lt;template&gt;  &lt;div&gt;    &lt;Post&gt;      &lt;template v-slot:name&gt;         &lt;h1&gt;What is Lorem Ipsum?&lt;/h1&gt;      &lt;/template&gt;      &lt;template v-slot:description&gt;        &lt;p&gt;          What is Lorem Ipsum?Lorem Ipsum is simply dummy text of the printing          andtypesetting industry.Lorem Ipsum has been the industry's standard          dummy.       &lt;/p&gt;     &lt;/template&gt;    &lt;/Post&gt;  &lt;/div&gt;&lt;/template&gt;&lt;script&gt;import Post from "./components/Post";export default {  components: {    Post  }};&lt;/script&gt;

Fallback data

In some cases, we can use fallback data (aka default) when data is not passed to a slot.

For example:

my-button.vue
&lt;template&gt;   &lt;button&gt;       &lt;slot&gt;Submit&lt;/slot&gt;   &lt;/button&gt;&lt;/template&gt;

In the above component, we have added a Submit text inside a slot element.

Now, If we use a my-button component without passing any data we can seethe fallback data Submit text is rendered inside the button.

&lt;template&gt;  &lt;div&gt;     &lt;my-button&gt;&lt;/my-button&gt;  &lt;/div&gt;&lt;/template&gt;&lt;script&gt;import Mybutton from "./components/my-button.vue";export default {  components: {    "my-button": Mybutton  }};&lt;/script&gt;

Output of rendered html:

&lt;div&gt;   &lt;button&gt;Submit&lt;/button&gt;&lt;/div&gt;

But, if we pass data to the my-button component fallback data is replaced.

&lt;template&gt;  &lt;div&gt;     &lt;my-button&gt;Add&lt;/my-button&gt;  &lt;/div&gt;&lt;/template&gt;&lt;script&gt;import Mybutton from "./components/my-button.vue";export default {  components: {    "my-button": Mybutton  }};&lt;/script&gt;

Output of rendered html:

&lt;div&gt;   &lt;button&gt;Add&lt;/button&gt;&lt;/div&gt;

Hope this code and post will helped you for implement How to use the slots in Vue.js. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US