]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): handle KeepAlive children unmount when wrapped in stable v-for
authordaiwei <daiwei521@126.com>
Wed, 19 Feb 2025 14:01:35 +0000 (22:01 +0800)
committerdaiwei <daiwei521@126.com>
Thu, 20 Feb 2025 03:22:23 +0000 (11:22 +0800)
packages/runtime-core/__tests__/rendererOptimizedMode.spec.ts
packages/runtime-core/src/renderer.ts

index 958c1274806c6b93563ffee36355282b98e62fa2..5496cd83058d91970422d0403336708810aa2ce6 100644 (file)
@@ -1,10 +1,12 @@
 import {
   Fragment,
   type FunctionalComponent,
+  KeepAlive,
   type SetupContext,
   Teleport,
   type TestElement,
   type VNode,
+  computed,
   createApp,
   createBlock,
   createCommentVNode,
@@ -1294,4 +1296,51 @@ describe('renderer: optimized mode', () => {
     expect(inner(root)).toBe('<!--v-if-->')
     expect(beforeUnmountSpy).toHaveBeenCalledTimes(1)
   })
+
+  //#12914
+  test('unmount KeepAlive children when wrapped in v-for with stable fragment', async () => {
+    const CompA = {
+      setup() {
+        return () => h('span', 'CompA')
+      },
+    }
+    const CompB = {
+      setup() {
+        return () => h('span', 'CompB')
+      },
+    }
+
+    const toggle = ref(true)
+    const view = computed(() => {
+      return toggle.value ? CompA : CompB
+    })
+
+    const app = createApp({
+      render() {
+        return (
+          openBlock(),
+          createElementBlock(
+            Fragment,
+            null,
+            renderList(1, () => {
+              return createVNode(
+                KeepAlive,
+                null,
+                [(openBlock(), createBlock(view.value))],
+                1024 /* DYNAMIC_SLOTS */,
+              )
+            }),
+            64 /* STABLE_FRAGMENT */,
+          )
+        )
+      },
+    })
+
+    app.mount(root)
+    expect(inner(root)).toBe('<span>CompA</span>')
+
+    toggle.value = false
+    await nextTick()
+    expect(inner(root)).toBe('<span>CompB</span>')
+  })
 })
index 05c4ac345eb81f9e030bc2078febc24c22f1dfe6..ccf99f6b4a6d8eae1c46d2025f82937b715a29a5 100644 (file)
@@ -2107,7 +2107,7 @@ function baseCreateRenderer(
     }
 
     if (shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE) {
-      ;(parentComponent!.ctx as KeepAliveContext).deactivate(vnode)
+      ;(vnode.component!.parent!.ctx as KeepAliveContext).deactivate(vnode)
       return
     }