createSlots.ts 839 B

123456789101112131415161718192021222324252627282930313233
  1. import { Slot } from '../componentSlots'
  2. import { isArray } from '@vue/shared'
  3. interface CompiledSlotDescriptor {
  4. name: string
  5. fn: Slot
  6. }
  7. /**
  8. * Compiler runtime helper for creating dynamic slots object
  9. * @private
  10. */
  11. export function createSlots(
  12. slots: Record<string, Slot>,
  13. dynamicSlots: (
  14. | CompiledSlotDescriptor
  15. | CompiledSlotDescriptor[]
  16. | undefined)[]
  17. ): Record<string, Slot> {
  18. for (let i = 0; i < dynamicSlots.length; i++) {
  19. const slot = dynamicSlots[i]
  20. // array of dynamic slot generated by <template v-for="..." #[...]>
  21. if (isArray(slot)) {
  22. for (let j = 0; j < slot.length; j++) {
  23. slots[slot[j].name] = slot[j].fn
  24. }
  25. } else if (slot) {
  26. // conditional single slot generated by <template v-if="..." #foo>
  27. slots[slot.name] = slot.fn
  28. }
  29. }
  30. return slots
  31. }