Slots Vue Js

Posted onby admin

In this article, we will get a full understanding of the vue slots through the practical application of its various use cases. Lets start with know about vuejs slots first.

If a slotis defined without a nameattribute then any content which is placed within component tags not specifying a slotattribute will be placed into that slot. See the multi insertionexample on the Vue.js official docs. PDF- Download Vue.jsfor free. The mechanism of slots in Vue.js makes it easier to create universal, reusable components. Slots are very powerful and at first may seem hard to use, especially the more advanced variants. In this article, I will present a reusable grid component using scoped slots with dynamic names, but to make it easier to understand, I will start with some. Vue.js has a feature called slots. In this video I discuss how slots work and some tips you should know!#vuejs #vuejs3 #learnvue #vueslots👉Check out my last.

What is Vue Slot?

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.
  • Start building web applications and products using our Free Vuejs Templates without any investment.

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 it is possible for us to pass HTML elements from a parent to its child component as a string this will make our website vulnerable to cross-site scripting attack that is why vuejs provides us with a slot which is a more secure and reliable method of passing HTML element 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:

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 andchild.vue files

Adding the code below to child.vue

Add the code snippet below to parent.vue

Add the code snippet below to parent.vue

Slots

Here we imported the child component and pass down the HTML content to the child.

For these contents to be displayed in the child component, theslot tag must be added to the child component.

Lets add the slot tag to the child.vue file as follow:

In the app.js file add the parent.vue component

Now, we can verify that slot is working as expected.

Now our app should be like:

STYLING SLOT COMPONENT

Vue Slot Prop

For styling our slot component, the CSS styles should be added to the component 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.

Using Multiple Slots

In order to use multiple slots in vue, vuejs provides us with away 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:

Vue Jsx Slots

In the child.vue component we will receive the named slot as follows:

Vue Template Slot

Here vuejs reserves two slots to display the content of the slotattribute with the value of message and name as separate contents.

Conclusion

In this article, we have seen a practical use case of slots to transfer content from a parent component to a child component.

For more information on the slot, check out the Vue documentation.

Tutorial

While this tutorial has content that we believe is of great benefit to our community, we have not yet tested or edited it to ensure you have an error-free learning experience. It's on our list, and we're working on it! You can help us out by using the 'report an issue' button at the bottom of the tutorial.

Vue

While basic component slots are all fine and dandy, sometimes you want the template inside the slot to be able to access data from the child component hosting the slot content. For example, if you’re trying to allow custom templates in a container while still retaining access to those containers’ data properties, you’ll want to use a scoped slot.

Introduction

Scoped slots are a new feature introduced in Vue 2.1.0. They allow you to pass properties from your child components into a scoped slot, and access them from the parent. Sort of like reverse property passing.

The first step to creating a scoped component slot is to pass properties into a default or named slot from your child component, like so:

Then, to use those properties inside a parent component’s slot content, create a template element tied to the slot. Add a scope attribute to the template element and set it to the name that you wish to access the scope properties from. This essentially creates a local variable for anything inside that template, allowing you to access it as if it was in the parent’s scope.

(For example, scope=“myScope”, properties passed into the <slot> will be accessible as {{myScope.myProperty}} while scope=“yourScope” will be accessed as {{yourScope.myProperty}}.)

Note: The template element does not get added to the DOM. It’s simply a wrapper.

ParentComponent.vue

If you’re using Vue 2.5 or above, use the slot-scope attribute instead of scope. Also you can skip the template elements.