Vue Slot Props Example

  

Next Up, Let’s review Named Slots in VueJS with the help of an example. We covered a lesson on slots in VueJS in the last section. slots help us to take the defined content inside the HTML markup of the component and put them in wherever specified in the template. The problem is it only works for a single default slot.

But what if we want to create more complex markup, where we are looking to accept different content from the user which we have to place at different locations.

New Vue Composition API overview and comparison with classic Vue Options-based API; Examples to implement a Vue Component with new API: props, data, watchers, lifecycle hooks; Example to take advantage of new Vue 3 Composition API (function-based API): split Topics into Functions; Related Post: Vue v-slot tutorial with examples. Here vuejs reserves two slots to display the content of the slot attribute with the value of message and name as separate contents. 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.

  • The child component contains a prop called 'signal'. I would like to be able to change data called 'parentVal' in the parent component so that the children's signal prop is updated with the parent's value. This seems like it should be something simple, but I cannot figure out how to do this using slots: Here is a running example below.
  • Vue.js simplified: components, props, and slots December 10, 2020 8 min read 2425 In this article, we will be using Vue.js, Vue CLI and Bootstrap CSS.
Vue

Let’s understand this with the help of an example.

Vue.js - The Progressive JavaScript Framework. One-Way Data Flow. All props form a one-way-down binding between the child property and the parent one: when the parent property updates, it will flow down to the child, but not the other way around. This prevents child components from accidentally mutating the parent's state, which can make your app's data flow harder to understand.

We are looking to convert the Bootstrap Card markup to a Vue Component. Here is generic HTML markup of a Bootstrap card.

And this gives out a generic card styling in the output.

Slot

We want the user to be able to specify content to be slotted in the card header, card body, and card footer.

Named Slots in VueJS

Let’s approach it using named slots in VueJS.

Here is the Vue Component code, we have defined the Bootstrap Card markup in our HTML and we have replaced the text with slot element.

Notice that the slots are named. The name of the first slot is header, The second one is without a slot that means this is the default slot, the name of the third slot is the footer.

Now let’s see how we can pas content to different slots from our HTML markup.

We have defined the first template with v-slot:header directive inside the component markup. This denotes that text that should go into the header slot. There is a similar template with v-slot:footer directive which denotes the content that should go into footer slot.

Also, there is HTML content defined without any directive which gives an indication to VueJS to put it inside the default slot.

Fallback Content

VueJS slots also have provision for fallback content is no content is supplied to the slot.

Notice our VueJS component for footer slot we have defined a fallback content.

Vue Props Types

If no content is supplied to the footer slot from the markup, then the slot will be replaced with the default content provided.

Vue Props Update

That’s all about Named Slots in VueJS.

Related Articles