Przeglądaj źródła

handle intermittent key presence during patch

Evan You 10 lat temu
rodzic
commit
4be4b897a7
1 zmienionych plików z 11 dodań i 1 usunięć
  1. 11 1
      src/core/vdom/patch.js

+ 11 - 1
src/core/vdom/patch.js

@@ -215,6 +215,14 @@ export function createPatchFunction (backend) {
     }
   }
 
+  function findOldIdx (vnode, oldCh, i, l) {
+    while (i++ < l) {
+      if (isDef(oldCh[i]) && sameVnode(vnode, oldCh[i])) {
+        return i
+      }
+    }
+  }
+
   function updateChildren (parentElm, oldCh, newCh, insertedVnodeQueue) {
     let oldStartIdx = 0
     let newStartIdx = 0
@@ -251,7 +259,9 @@ export function createPatchFunction (backend) {
         newStartVnode = newCh[++newStartIdx]
       } else {
         if (isUndef(oldKeyToIdx)) oldKeyToIdx = createKeyToOldIdx(oldCh, oldStartIdx, oldEndIdx)
-        idxInOld = oldKeyToIdx[newStartVnode.key]
+        idxInOld = isDef(newStartVnode.key)
+          ? oldKeyToIdx[newStartVnode.key]
+          : findOldIdx(newStartVnode, oldCh, oldStartIdx, oldEndIdx)
         if (isUndef(idxInOld)) { // New element
           nodeOps.insertBefore(parentElm, createElm(newStartVnode, insertedVnodeQueue), oldStartVnode.elm)
           newStartVnode = newCh[++newStartIdx]