Trung BìnhVue.js iconVue.js

Slots là gì? Named slots và scoped slots?

Slots cho phép parent inject content vào template của child component.

  • Default slot: <slot />.
  • Named slots: <slot name="header" /> — parent dùng <template #header>.
  • Scoped slot: child truyền data lên parent qua slot:
vue
<!-- Child -->
<slot :item="item" :index="i" />

<!-- Parent -->
<template #default="{ item, index }">
  <span>{{ index }}: {{ item.name }}</span>
</template>

Dùng scoped slots khi child biết cách lấy data nhưng parent quyết định cách render (render prop pattern).

Pitfall: không mix slot và v-if trên cùng <template> — tách riêng.

Xem toàn bộ Vue.js cùng filter theo level & chủ đề con.

Mở danh sách Vue.js