patchVNode 函数中对新旧节点类型的判断
来源:5-8 组件更新(2)

ominus3
2021-02-18
老师您好,我的问题在文档中对应这里的内容
const oldCh = oldVnode.children
const ch = vnode.children
if (isUndef(vnode.text)) {
if (isDef(oldCh) && isDef(ch)) {
if (oldCh !== ch) updateChildren(elm, oldCh, ch, insertedVnodeQueue, removeOnly)
} else if (isDef(ch)) {
if (isDef(oldVnode.text)) nodeOps.setTextContent(elm, '')
addVnodes(elm, null, ch, 0, ch.length - 1, insertedVnodeQueue)
} else if (isDef(oldCh)) {
removeVnodes(elm, oldCh, 0, oldCh.length - 1)
} else if (isDef(oldVnode.text)) {
nodeOps.setTextContent(elm, '')
}
} else if (oldVnode.text !== vnode.text) {
nodeOps.setTextContent(elm, vnode.text)
}
我理解的是走到 patchVnode 函数的两个 vnode 说明他们是两个同类型的节点了,第一个逻辑 if (isUndef(vnode.text)) 不就已经表明了 vnode 不是一个文本节点了么,不就也说明 oldVnode 也不是一个文本节点了吗,为什么在下面还有两处 isDef(oldVnode.text) 的判断呢?
写回答
1回答
-
isUndef(vnode.text) 表示新的 vnode 节点不是一个文本节点,但不代表 oldVnode 节点不是一个文本节点,因为 sameVnode 只是 key 和 tag 相同,children 不一定相同喔
052021-02-19
相似问题