vue.ts 1002 字节
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { ComponentInternalInstance, VNode } from 'vue'
fxy060608's avatar
fxy060608 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import { hyphenate } from '@vue/shared'

import { isBuiltInComponent } from './tags'

export function resolveOwnerVm(vm: ComponentInternalInstance) {
  if (!vm) {
    return
  }
  let componentName = vm.type.name
  while (componentName && isBuiltInComponent(hyphenate(componentName))) {
    // ownerInstance 内置组件需要使用父 vm
    vm = vm.parent!
    componentName = vm.type.name
  }
  return vm.proxy!
}
fxy060608's avatar
fxy060608 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

function isElement(el: Element) {
  // Element
  return el.nodeType === 1
}

export function resolveOwnerEl(instance: ComponentInternalInstance) {
  const { vnode } = instance
  if (isElement(vnode.el as Element)) {
    return vnode.el
  }
  const { subTree } = instance
  // ShapeFlags.ARRAY_CHILDREN = 1<<4
  if (subTree.shapeFlag & 16) {
    const elemVNode = (subTree.children as VNode[]).find((vnode) =>
      isElement(vnode.el as Element)
    )
    if (elemVNode) {
      return elemVNode.el
    }
  }
  return vnode.el
}