Browse Source

warn against duplicate keys

dev
Evan You 9 years ago
parent
commit
cec2196657
  1. 8
      benchmarks/reorder-list/index.html
  2. 8
      src/runtime/vdom/patch.js

8
benchmarks/reorder-list/index.html

@ -8,6 +8,7 @@
<script type="text/x-template" id="t"> <script type="text/x-template" id="t">
<div> <div>
<h1>{{ total }} Components</h1>
<p>{{ action }} took {{time}}ms.</p> <p>{{ action }} took {{time}}ms.</p>
<button @click="shuffle">shuffle</button> <button @click="shuffle">shuffle</button>
<button @click="add">add</button> <button @click="add">add</button>
@ -34,13 +35,13 @@
</tr> </tr>
</script> </script>
<h1>1000 Components</h1>
<div id="el"> <div id="el">
</div> </div>
<script> <script>
var total = 1000
var items = [] var items = []
for (var i = 0; i < 1000; i++) { for (var i = 0; i < total; i++) {
items.push({ items.push({
id: i, id: i,
label: String(Math.random()).slice(0, 5) label: String(Math.random()).slice(0, 5)
@ -52,6 +53,7 @@ var vm = new Vue({
el: '#el', el: '#el',
template: '#t', template: '#t',
data: { data: {
total: total,
time: 0, time: 0,
action: 'Render', action: 'Render',
items: items, items: items,
@ -63,7 +65,7 @@ var vm = new Vue({
}), }),
add: monitor('add', function () { add: monitor('add', function () {
this.items.push({ this.items.push({
id: this.items.length, id: total++,
label: String(Math.random()).slice(0, 5) label: String(Math.random()).slice(0, 5)
}) })
}), }),

8
src/runtime/vdom/patch.js

@ -1,6 +1,6 @@
import VNode from './vnode' import VNode from './vnode'
import * as dom from './dom' import * as dom from './dom'
import { isPrimitive } from '../util/index' import { isPrimitive, warn } from '../util/index'
const emptyNode = VNode('', {}, []) const emptyNode = VNode('', {}, [])
const hooks = ['create', 'update', 'remove', 'destroy', 'pre', 'post'] const hooks = ['create', 'update', 'remove', 'destroy', 'pre', 'post']
@ -178,6 +178,12 @@ export default function createPatchFunction (modules, api) {
newStartVnode = newCh[++newStartIdx] newStartVnode = newCh[++newStartIdx]
} else { } else {
elmToMove = oldCh[idxInOld] elmToMove = oldCh[idxInOld]
if (process.env.NODE_ENV !== 'production' && !elmToMove) {
warn(
'Duplicate track-by key: ' + idxInOld + '. ' +
'Make sure each v-for item has a unique track-by key.'
)
}
patchVnode(elmToMove, newStartVnode, insertedVnodeQueue) patchVnode(elmToMove, newStartVnode, insertedVnodeQueue)
oldCh[idxInOld] = undefined oldCh[idxInOld] = undefined
api.insertBefore(parentElm, getElm(elmToMove), getElm(oldStartVnode)) api.insertBefore(parentElm, getElm(elmToMove), getElm(oldStartVnode))

Loading…
Cancel
Save