fix(runtime-core): properly check forwarded slots type (#3781)

fix #3779
This commit is contained in:
HcySunYang 2021-05-26 23:52:03 +08:00 committed by GitHub
parent 4e3f82f683
commit e8ddf86080
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 3 deletions

View File

@ -734,4 +734,54 @@ describe('renderer: optimized mode', () => {
await nextTick() await nextTick()
expect(inner(root)).toBe('<p>1</p>') expect(inner(root)).toBe('<p>1</p>')
}) })
// #3779
test('treat slots manually written by the user as dynamic', async () => {
const Middle = {
setup(props: any, { slots }: any) {
return slots.default!
}
}
const Comp = {
setup(props: any, { slots }: any) {
return () => {
return (
openBlock(),
createBlock('div', null, [
createVNode(Middle, null, {
default: withCtx(
() => [
createVNode('div', null, [renderSlot(slots, 'default')])
],
undefined
),
_: 3 /* FORWARDED */
})
])
)
}
}
}
const loading = ref(false)
const app = createApp({
setup() {
return () => {
// important: write the slot content here
const content = h('span', loading.value ? 'loading' : 'loaded')
return h(Comp, null, {
default: () => content
})
}
}
})
app.mount(root)
expect(inner(root)).toBe('<div><div><span>loaded</span></div></div>')
loading.value = true
await nextTick()
expect(inner(root)).toBe('<div><div><span>loading</span></div></div>')
})
}) })

View File

@ -651,12 +651,12 @@ export function normalizeChildren(vnode: VNode, children: unknown) {
// a child component receives forwarded slots from the parent. // a child component receives forwarded slots from the parent.
// its slot type is determined by its parent's slot type. // its slot type is determined by its parent's slot type.
if ( if (
currentRenderingInstance.vnode.patchFlag & PatchFlags.DYNAMIC_SLOTS (currentRenderingInstance.slots as RawSlots)._ === SlotFlags.STABLE
) { ) {
;(children as RawSlots)._ = SlotFlags.STABLE
} else {
;(children as RawSlots)._ = SlotFlags.DYNAMIC ;(children as RawSlots)._ = SlotFlags.DYNAMIC
vnode.patchFlag |= PatchFlags.DYNAMIC_SLOTS vnode.patchFlag |= PatchFlags.DYNAMIC_SLOTS
} else {
;(children as RawSlots)._ = SlotFlags.STABLE
} }
} }
} }