@@ -249,8 +249,8 @@ Here's a complete example of a simple todo list:
249
249
<li
250
250
is =" todo-item"
251
251
v-for =" (todo, index) in todos"
252
- v-bind:key =" index "
253
- v-bind:title =" todo"
252
+ v-bind:key =" todo.id "
253
+ v-bind:title =" todo.title "
254
254
v-on:remove =" todos.splice(index, 1)"
255
255
></li >
256
256
</ul >
@@ -273,22 +273,35 @@ new Vue({
273
273
data: {
274
274
newTodoText: ' ' ,
275
275
todos: [
276
- ' Do the dishes' ,
277
- ' Take out the trash' ,
278
- ' Mow the lawn'
279
- ]
276
+ {
277
+ id: 1 ,
278
+ title: ' Do the dishes' ,
279
+ },
280
+ {
281
+ id: 2 ,
282
+ title: ' Take out the trash' ,
283
+ },
284
+ {
285
+ id: 3 ,
286
+ title: ' Mow the lawn'
287
+ }
288
+ ],
289
+ nextTodoId: 4
280
290
},
281
291
methods: {
282
292
addNewTodo : function () {
283
- this .todos .push (this .newTodoText )
293
+ this .todos .push ({
294
+ id: this .nextTodoId ++ ,
295
+ title: this .newTodoText
296
+ })
284
297
this .newTodoText = ' '
285
298
}
286
299
}
287
300
})
288
301
```
289
302
290
303
{% raw %}
291
- <div id =" todo-list-example " class = " demo " >
304
+ <div id =" todo-list-example " >
292
305
<input
293
306
v-model="newTodoText"
294
307
v-on: keyup .enter="addNewTodo"
@@ -298,8 +311,8 @@ new Vue({
298
311
<li
299
312
is="todo-item"
300
313
v-for="(todo, index) in todos"
301
- v-bind:key="index "
302
- v-bind:title="todo"
314
+ v-bind:key="todo.id "
315
+ v-bind:title="todo.title "
303
316
v-on:remove="todos.splice(index, 1)"
304
317
></li>
305
318
</ul >
@@ -314,19 +327,33 @@ Vue.component('todo-item', {
314
327
' ,
315
328
props: [' title' ]
316
329
})
330
+
317
331
new Vue ({
318
332
el: ' #todo-list-example' ,
319
333
data: {
320
334
newTodoText: ' ' ,
321
335
todos: [
322
- ' Do the dishes' ,
323
- ' Take out the trash' ,
324
- ' Mow the lawn'
325
- ]
336
+ {
337
+ id: 1 ,
338
+ title: ' Do the dishes' ,
339
+ },
340
+ {
341
+ id: 2 ,
342
+ title: ' Take out the trash' ,
343
+ },
344
+ {
345
+ id: 3 ,
346
+ title: ' Mow the lawn'
347
+ }
348
+ ],
349
+ nextTodoId: 4
326
350
},
327
351
methods: {
328
352
addNewTodo : function () {
329
- this .todos .push (this .newTodoText )
353
+ this .todos .push ({
354
+ id: this .nextTodoId ++ ,
355
+ title: this .newTodoText
356
+ })
330
357
this .newTodoText = ' '
331
358
}
332
359
}
0 commit comments