Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

fix(runtime-vapor): current instance is not attached to static slots #247

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/runtime-vapor/__tests__/componentSlots.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,40 @@ describe('component: slots', () => {
})

test('the current instance should be kept in the slot', async () => {
let instanceInDefaultSlot: any
let instanceInFooSlot: any

const Comp = defineComponent({
render() {
const instance = getCurrentInstance()
instance!.slots.default!()
instance!.slots.foo!()
return template('<div></div>')()
},
})

const { instance } = define({
render() {
return createComponent(Comp, {}, [
{
default: () => {
instanceInDefaultSlot = getCurrentInstance()
return template('content')()
},
foo: () => {
instanceInFooSlot = getCurrentInstance()
return template('content')()
},
},
])
},
}).render()

expect(instanceInDefaultSlot).toBe(instance)
expect(instanceInFooSlot).toBe(instance)
})

test('the current instance should be kept in the dynamic slots', async () => {
let instanceInDefaultSlot: any
let instanceInVForSlot: any
let instanceInVIfSlot: any
Expand Down
10 changes: 8 additions & 2 deletions packages/runtime-vapor/src/componentSlots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@ export function initSlots(
if (!rawSlots) return
if (!isArray(rawSlots)) rawSlots = [rawSlots]

if (rawSlots.length === 1 && !isDynamicSlotFn(rawSlots[0])) {
instance.slots = rawSlots[0]
const hasDynamicSlot = rawSlots.some(slot => isDynamicSlotFn(slot))
if (!hasDynamicSlot) {
instance.slots = {}
// with ctx
const slots = rawSlots[0] as StaticSlots
for (const name in slots) {
registerSlot(name, slots[name])
}
return
}

Expand Down