I took me many examples and a lot of practice to understand slots. Whether it’s simple slots, named slots or scoped slots, you will find that it’s a powerful feature in Vue. Let’s dive into it.
The challenge
At the end of last month, I attempted to solve a small project while learning Vue.js concepts.
The project required to build a photo slider that used 2 child components in the App.vue. The SwiperSlide below finds itself in a scoped slot.
The slot resides in the Swiper component as you see with v-slot="{ item }".
First, the prop name on the <slot> element is key: it must match what the parent provides to the slot, in our case it is v-slot="{ item }". So the prop must be named :item.
Second, the actual image object must be destructured so that the props definition in SwiperSlide receive them individually.
If I’m mistaken somewhere, tell me so I can correct the wording, but after trying it out, I’m pretty sure that I now understand how scoped slots work.