1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| function createNewDom(vnode, parent, comp, olddomOrComp, myIndex) { ... setAttrs(dom, vnode.props)
setNewDom(parent, dom, myIndex) // 创建时根据myIndex 决定是append / replace
for(let i = 0; i < vnode.children.length; i++) { render(vnode.children[i], dom, null, null, i) // myIndex 其实就是 children 的 i } } function diffDOM(vnode, parent, comp, olddom) { ... const willRemoveArr = olddom.__rendered.slice(vnode.children.length) // 将要删除的 dom const renderedArr = olddom.__rendered.slice(0, vnode.children.length)
olddom.__rendered = renderedArr for(let i = 0; i < vnode.children.length; i++) { render(vnode.children[i], olddom, null, renderedArr[i], i) // 同样要增加 children 的 i } ... }
|