apiCreateComponent.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import {
  2. type Component,
  3. ComponentInternalInstance,
  4. currentInstance,
  5. } from './component'
  6. import { setupComponent } from './apiRender'
  7. import type { RawProps } from './componentProps'
  8. import type { RawSlots } from './componentSlots'
  9. import { withAttrs } from './componentAttrs'
  10. import { isString } from '@vue/shared'
  11. import { fallbackComponent } from './componentFallback'
  12. export function createComponent(
  13. comp: Component | string,
  14. rawProps: RawProps | null = null,
  15. slots: RawSlots | null = null,
  16. singleRoot: boolean = false,
  17. once: boolean = false,
  18. ): ComponentInternalInstance | HTMLElement {
  19. const current = currentInstance!
  20. if (isString(comp)) {
  21. return fallbackComponent(comp, rawProps, slots, current, singleRoot)
  22. }
  23. const instance = new ComponentInternalInstance(
  24. comp,
  25. singleRoot ? withAttrs(rawProps) : rawProps,
  26. slots,
  27. once,
  28. )
  29. if (singleRoot) {
  30. instance.scopeIds.push(...current.scopeIds)
  31. }
  32. const scopeId = current.type.__scopeId
  33. if (scopeId) {
  34. instance.scopeIds.push(scopeId)
  35. }
  36. setupComponent(instance)
  37. // register sub-component with current component for lifecycle management
  38. // current.comps.add(instance)
  39. return instance
  40. }