renderSlot.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { Data } from '../component'
  2. import { Slots } from '../componentSlots'
  3. import {
  4. VNodeArrayChildren,
  5. openBlock,
  6. createBlock,
  7. Fragment,
  8. VNode
  9. } from '../vnode'
  10. import { PatchFlags } from '@vue/shared'
  11. import { warn } from '../warning'
  12. /**
  13. * Compiler runtime helper for rendering <slot/>
  14. * @internal
  15. */
  16. export function renderSlot(
  17. slots: Slots,
  18. name: string,
  19. props: Data = {},
  20. // this is not a user-facing function, so the fallback is always generated by
  21. // the compiler and guaranteed to be a function returning an array
  22. fallback?: () => VNodeArrayChildren
  23. ): VNode {
  24. let slot = slots[name]
  25. if (__DEV__ && slot && slot.length > 1) {
  26. warn(
  27. `SSR-optimized slot function detected in a non-SSR-optimized render ` +
  28. `function. You need to mark this component with $dynamic-slots in the ` +
  29. `parent template.`
  30. )
  31. slot = () => []
  32. }
  33. return (
  34. openBlock(),
  35. createBlock(
  36. Fragment,
  37. { key: props.key },
  38. slot ? slot(props) : fallback ? fallback() : [],
  39. slots._ ? PatchFlags.STABLE_FRAGMENT : PatchFlags.BAIL
  40. )
  41. )
  42. }