]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(hmr/transition): fix kept-alive component inside transition disappearing after...
author被雨水过滤的空气-Rain <958414905@qq.com>
Thu, 9 Nov 2023 09:15:56 +0000 (17:15 +0800)
committerGitHub <noreply@github.com>
Thu, 9 Nov 2023 09:15:56 +0000 (17:15 +0800)
fix #7121

packages/runtime-core/__tests__/hmr.spec.ts
packages/runtime-core/src/components/BaseTransition.ts

index 2e989e368a331383a0fc7d4766d6c7fc2765f47d..a5ec90ad7d3c81c750b9e3ec1496924e6f23b28b 100644 (file)
@@ -218,6 +218,75 @@ describe('hot module replacement', () => {
     expect(deactiveSpy).toHaveBeenCalledTimes(1)
   })
 
+  // #7121
+  test('reload KeepAlive slot in Transition', async () => {
+    const root = nodeOps.createElement('div')
+    const childId = 'test-transition-keep-alive-reload'
+    const unmountSpy = vi.fn()
+    const mountSpy = vi.fn()
+    const activeSpy = vi.fn()
+    const deactiveSpy = vi.fn()
+
+    const Child: ComponentOptions = {
+      __hmrId: childId,
+      data() {
+        return { count: 0 }
+      },
+      unmounted: unmountSpy,
+      render: compileToFunction(`<div>{{ count }}</div>`)
+    }
+    createRecord(childId, Child)
+
+    const Parent: ComponentOptions = {
+      components: { Child },
+      data() {
+        return { toggle: true }
+      },
+      render: compileToFunction(
+        `<button @click="toggle = !toggle"></button><BaseTransition mode="out-in"><KeepAlive><Child v-if="toggle" /></KeepAlive></BaseTransition>`
+      )
+    }
+
+    render(h(Parent), root)
+    expect(serializeInner(root)).toBe(`<button></button><div>0</div>`)
+
+    reload(childId, {
+      __hmrId: childId,
+      data() {
+        return { count: 1 }
+      },
+      mounted: mountSpy,
+      unmounted: unmountSpy,
+      activated: activeSpy,
+      deactivated: deactiveSpy,
+      render: compileToFunction(`<div>{{ count }}</div>`)
+    })
+    await nextTick()
+    expect(serializeInner(root)).toBe(`<button></button><div>1</div>`)
+    expect(unmountSpy).toHaveBeenCalledTimes(1)
+    expect(mountSpy).toHaveBeenCalledTimes(1)
+    expect(activeSpy).toHaveBeenCalledTimes(1)
+    expect(deactiveSpy).toHaveBeenCalledTimes(0)
+
+    // should not unmount when toggling
+    triggerEvent(root.children[1] as TestElement, 'click')
+    await nextTick()
+    expect(serializeInner(root)).toBe(`<button></button><!---->`)
+    expect(unmountSpy).toHaveBeenCalledTimes(1)
+    expect(mountSpy).toHaveBeenCalledTimes(1)
+    expect(activeSpy).toHaveBeenCalledTimes(1)
+    expect(deactiveSpy).toHaveBeenCalledTimes(1)
+
+    // should not mount when toggling
+    triggerEvent(root.children[1] as TestElement, 'click')
+    await nextTick()
+    expect(serializeInner(root)).toBe(`<button></button><div>1</div>`)
+    expect(unmountSpy).toHaveBeenCalledTimes(1)
+    expect(mountSpy).toHaveBeenCalledTimes(1)
+    expect(activeSpy).toHaveBeenCalledTimes(2)
+    expect(deactiveSpy).toHaveBeenCalledTimes(1)
+  })
+
   test('reload class component', async () => {
     const root = nodeOps.createElement('div')
     const childId = 'test4-child'
index 9cb80b94ef0146c4fd806a6dc91c8e424cc299a2..8711d7271c287d11bc6383d619d2b359b7729176 100644 (file)
@@ -473,7 +473,11 @@ function emptyPlaceholder(vnode: VNode): VNode | undefined {
 
 function getKeepAliveChild(vnode: VNode): VNode | undefined {
   return isKeepAlive(vnode)
-    ? vnode.children
+    ? // #7121 ensure get the child component subtree in case
+      // it's been replaced during HMR
+      __DEV__ && vnode.component
+      ? vnode.component.subTree
+      : vnode.children
       ? ((vnode.children as VNodeArrayChildren)[0] as VNode)
       : undefined
     : vnode