Vue Slot Props
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.
What is Vue Slot?
You can achieve what you want by using render function, and iterate through the VNodes in this.$slots.default, find nodes with vnode.tag 'grid' add appropriate props to them. This requires you to be a bit familiar with the VNode structure, but there isn't anything extra that Vue needs to expose to allow that.
- Create a component that initializes our map. In the template, we create a container for the map.
- If you have no idea how to proceed, the good news is that after reading this article, you will understand most of the things you should know about a handy Vue.js feature: slots. When I started with Vue.js, I was not using slots very much. But to be honest with you, now, it’s one of my favorite features.
- If you're Using VueJS2 and like to use JSX along with it. In this case,to use the slot, the solution with example is below.We have to use this.$slots.defaultIt's almost like this.props.childrenin React 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.
- 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:
Vue Get Slot Props
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
Vue Slot Props Computed
Adding the code below to child.vue
Add the code snippet below to parent.vue
Add the code snippet below to parent.vue
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:
Vue Component Slot
STYLING SLOT COMPONENT
For styling our slot component, the CSS styles should be added to the component with the slot tag.
Vue Slot Pass Props
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.
Vue Slot Event
In the Parent.vue component we will name our slots as follows:
In the child.vue component we will receive the named slot as follows:
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.