Forráskód Böngészése

fix(runtime-vapor): invoke onVnodeBeforeMount for vapor components in interop

daiwei 1 hónapja
szülő
commit
68c405c078

+ 36 - 0
packages/runtime-vapor/__tests__/vdomInterop.spec.ts

@@ -1965,6 +1965,42 @@ describe('vdomInterop', () => {
       expect(html()).toContain('<span>resolved</span>')
     })
   })
+  test('should invoke onVnodeBeforeMount/onVnodeBeforeUnmount on vapor child', async () => {
+    const beforeMountSpy = vi.fn()
+    const beforeUnmountSpy = vi.fn()
+
+    const VaporChild = defineVaporComponent({
+      setup() {
+        return template('<div>vapor</div>')()
+      },
+    })
+
+    const show = ref(true)
+    const App = defineComponent({
+      setup() {
+        return () =>
+          show.value
+            ? h(VaporChild as any, {
+                onVnodeBeforeMount: beforeMountSpy,
+                onVnodeBeforeUnmount: beforeUnmountSpy,
+              })
+            : null
+      },
+    })
+
+    const root = document.createElement('div')
+    const app = createApp(App)
+    app.use(vaporInteropPlugin)
+    app.mount(root)
+    await nextTick()
+
+    expect(beforeMountSpy).toHaveBeenCalledTimes(1)
+
+    // unmount
+    show.value = false
+    await nextTick()
+    expect(beforeUnmountSpy).toHaveBeenCalledTimes(1)
+  })
 
   describe('KeepAlive', () => {
     test('should update props on reactivation of vapor child in vdom KeepAlive', async () => {

+ 11 - 0
packages/runtime-vapor/src/vdomInterop.ts

@@ -212,6 +212,17 @@ const vaporInteropImpl: Omit<
       }
     }
 
+    // invoke onVnodeBeforeMount hook
+    const vnodeBeforeMountHook = vnode.props && vnode.props.onVnodeBeforeMount
+    if (vnodeBeforeMountHook) {
+      callWithAsyncErrorHandling(
+        vnodeBeforeMountHook,
+        parentComponent,
+        ErrorCodes.VNODE_HOOK,
+        [vnode],
+      )
+    }
+
     mountComponent(instance, container, selfAnchor)
 
     // invoke onVnodeMounted hook