Skip to content

Commit 499a7c3

Browse files
committed
fix: avoid other bugs
1 parent 7e1d2d4 commit 499a7c3

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

packages/runtime-core/__tests__/components/KeepAlive.spec.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,7 @@ describe('KeepAlive', () => {
887887
test('should invoke onActivated of child on initial mount', async () => {
888888
let parentCount = 0
889889
let childCount = 0
890+
const toggle = ref(true)
890891
const Child = defineComponent({
891892
name: 'Child',
892893
setup() {
@@ -908,7 +909,7 @@ describe('KeepAlive', () => {
908909

909910
const App = {
910911
render: () => {
911-
return h(KeepAlive, null, () => h(AsyncComp))
912+
return h(KeepAlive, null, () => (toggle.value ? h(AsyncComp) : null))
912913
}
913914
}
914915

@@ -917,6 +918,13 @@ describe('KeepAlive', () => {
917918
expect(serializeInner(root)).toBe('child')
918919
expect(parentCount).toBe(1)
919920
expect(childCount).toBe(1)
921+
922+
toggle.value = false
923+
await timeout()
924+
toggle.value = true
925+
await timeout()
926+
expect(parentCount).toBe(2)
927+
expect(childCount).toBe(2)
920928
})
921929

922930
// #4976

packages/runtime-core/src/components/KeepAlive.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -402,18 +402,10 @@ function registerKeepAliveHook(
402402
// arrays.
403403
if (target) {
404404
let current = target.parent
405-
let child = target
406405
while (current && current.parent) {
407406
if (isKeepAlive(current.parent.vnode)) {
408-
injectToKeepAliveRoot(
409-
wrappedHook,
410-
type,
411-
target,
412-
// #7276
413-
isAsyncWrapper(current) ? child : current
414-
)
407+
injectToKeepAliveRoot(wrappedHook, type, target, current)
415408
}
416-
child = current
417409
current = current.parent
418410
}
419411
}

packages/runtime-core/src/renderer.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,16 +1413,23 @@ function baseCreateRenderer(
14131413
)
14141414
}
14151415

1416+
const parentIsAsyncWrapperAndShouldKeepAlive =
1417+
parent &&
1418+
isAsyncWrapper(parent.vnode) &&
1419+
parent.vnode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
14161420
// activated hook for keep-alive roots.
14171421
// #1742 activated hook must be accessed after first render
14181422
// since the hook may be injected by a child keep-alive
14191423
if (
14201424
initialVNode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE ||
1421-
(parent &&
1422-
isAsyncWrapper(parent.vnode) &&
1423-
parent.vnode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE)
1425+
parentIsAsyncWrapperAndShouldKeepAlive
14241426
) {
1425-
instance.a && queuePostRenderEffect(instance.a, parentSuspense)
1427+
if (parentIsAsyncWrapperAndShouldKeepAlive) {
1428+
// #7276 - parent.a contains all the hooks of the descendants
1429+
parent.a && queuePostRenderEffect(parent.a, parentSuspense)
1430+
} else {
1431+
instance.a && queuePostRenderEffect(instance.a, parentSuspense)
1432+
}
14261433
if (
14271434
__COMPAT__ &&
14281435
isCompatEnabled(DeprecationTypes.INSTANCE_EVENT_HOOKS, instance)

0 commit comments

Comments
 (0)