vue3-core/packages/runtime-core/src/helpers/renderSlot.ts

42 lines
1.0 KiB
TypeScript
Raw Normal View History

2019-10-22 23:26:48 +08:00
import { Data } from '../component'
import { Slots } from '../componentSlots'
import {
VNodeArrayChildren,
openBlock,
createBlock,
Fragment,
VNode
} from '../vnode'
import { PatchFlags } from '@vue/shared'
2020-02-07 14:06:51 +08:00
import { warn } from '../warning'
2019-09-28 08:29:20 +08:00
export function renderSlot(
slots: Slots,
name: string,
2019-10-22 23:26:48 +08:00
props: Data = {},
2019-09-28 08:29:20 +08:00
// this is not a user-facing function, so the fallback is always generated by
// the compiler and guaranteed to be a function returning an array
fallback?: () => VNodeArrayChildren
): VNode {
2020-02-07 14:06:51 +08:00
let slot = slots[name]
if (__DEV__ && slot && slot.length > 1) {
2020-02-07 14:06:51 +08:00
warn(
`SSR-optimized slot function detected in a non-SSR-optimized render ` +
`function. You need to mark this component with $dynamic-slots in the ` +
`parent template.`
)
slot = () => []
}
return (
openBlock(),
createBlock(
Fragment,
{ key: props.key },
slot ? slot(props) : fallback ? fallback() : [],
slots._ ? PatchFlags.STABLE_FRAGMENT : PatchFlags.BAIL
)
)
2019-09-28 08:29:20 +08:00
}