Skip to content
Advertisement

VueJS – Pass slot to child of child component

I have a list and a list_item component that I reuse a lot inside my application. On a simplified form:

contact_list.vue

JavaScript

contact_list_item.vue

JavaScript

When I use contact_list inside a specific component, I want to be able to send a slot that will add some new columns to the contact_list_item component. This slot will use data of the specific contact that is being rendered inside that contact_list_item component to generate the new columns.

How could I achieve that? Is using slot the best approach?

Thanks in advance.

Advertisement

Answer

Slots are the best approach and you will need to use a scoped slot for the contact-list-item component. I’m not really familiar with pug, so I will use HTML for the example.

In contact-list you would add a slot. Notice in this case that the contact is being passed as a property. This is so we can take advantage of scoped slots.

JavaScript

Then add a slot to the contact-list-item.

JavaScript

Finally, in your Vue template, use the scoped template.

JavaScript

Here is a working example. I have no idea what your styles are but notice the id column is now displayed in the contact-list-item.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement