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

8
src/runtime/vdom/patch.js

@ -1,6 +1,6 @@
import VNode from './vnode'
import * as dom from './dom'
import { isPrimitive } from '../util/index'
import { isPrimitive, warn } from '../util/index'
const emptyNode = VNode('', {}, [])
const hooks = ['create', 'update', 'remove', 'destroy', 'pre', 'post']
@ -178,6 +178,12 @@ export default function createPatchFunction (modules, api) {
newStartVnode = newCh[++newStartIdx]
} else {
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)
oldCh[idxInOld] = undefined
api.insertBefore(parentElm, getElm(elmToMove), getElm(oldStartVnode))

Loading…
Cancel
Save