Skip to content

Commit 7d90c88

Browse files
LittleSoundsxzz
andauthored
fix(runtime-vapor): current instance is not attached to static slots (#247)
Co-authored-by: 三咲智子 Kevin Deng <[email protected]>
1 parent aa5fe96 commit 7d90c88

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

packages/runtime-vapor/__tests__/componentSlots.spec.ts

+34
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,40 @@ describe('component: slots', () => {
157157
})
158158

159159
test('the current instance should be kept in the slot', async () => {
160+
let instanceInDefaultSlot: any
161+
let instanceInFooSlot: any
162+
163+
const Comp = defineComponent({
164+
render() {
165+
const instance = getCurrentInstance()
166+
instance!.slots.default!()
167+
instance!.slots.foo!()
168+
return template('<div></div>')()
169+
},
170+
})
171+
172+
const { instance } = define({
173+
render() {
174+
return createComponent(Comp, {}, [
175+
{
176+
default: () => {
177+
instanceInDefaultSlot = getCurrentInstance()
178+
return template('content')()
179+
},
180+
foo: () => {
181+
instanceInFooSlot = getCurrentInstance()
182+
return template('content')()
183+
},
184+
},
185+
])
186+
},
187+
}).render()
188+
189+
expect(instanceInDefaultSlot).toBe(instance)
190+
expect(instanceInFooSlot).toBe(instance)
191+
})
192+
193+
test('the current instance should be kept in the dynamic slots', async () => {
160194
let instanceInDefaultSlot: any
161195
let instanceInVForSlot: any
162196
let instanceInVIfSlot: any

packages/runtime-vapor/src/componentSlots.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,17 @@ export function initSlots(
4040
if (!rawSlots) return
4141
if (!isArray(rawSlots)) rawSlots = [rawSlots]
4242

43-
if (rawSlots.length === 1 && !isDynamicSlotFn(rawSlots[0])) {
44-
instance.slots = rawSlots[0]
43+
if (!rawSlots.some(slot => isDynamicSlotFn(slot))) {
44+
instance.slots = {}
45+
// with ctx
46+
const slots = rawSlots[0] as StaticSlots
47+
for (const name in slots) {
48+
registerSlot(name, slots[name])
49+
}
4550
return
4651
}
4752

48-
const resolved: StaticSlots = (instance.slots = shallowReactive({}))
53+
instance.slots = shallowReactive({})
4954
const keys: Set<string>[] = []
5055
rawSlots.forEach((slots, index) => {
5156
const isDynamicSlot = isDynamicSlotFn(slots)
@@ -71,7 +76,7 @@ export function initSlots(
7176
: dynamicSlot && dynamicSlot.name === name)
7277
) {
7378
recordNames.delete(name)
74-
delete resolved[name]
79+
delete instance.slots[name]
7580
}
7681
}
7782
})
@@ -83,7 +88,7 @@ export function initSlots(
8388
})
8489

8590
function registerSlot(name: string, fn: Slot, recordNames?: Set<string>) {
86-
resolved[name] = withCtx(fn)
91+
instance.slots[name] = withCtx(fn)
8792
recordNames && recordNames.add(name)
8893
}
8994

0 commit comments

Comments
 (0)