1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Document</title>
    8. <script src="./lib/vue.js"></script>
    9. </head>
    10. <body>
    11. <div id="app">
    12. <div>
    13. <label>
    14. id:<input type="text" v-model="id">
    15. </label>
    16. <label>
    17. Name:<input type="text" v-model="name">
    18. </label>
    19. <input type="button" @click="add" value="添加">
    20. </div>
    21. <!-- v-for循环的时候,key属性只能使用number获取string. key使用的时候必须用v-bind或者简写: 指定key的值 -->
    22. <!-- v-for有问题,必须在key指定一个唯一的值(number/string). -->
    23. <p v-for="(item, index) in list" :key="item.id">
    24. <input type="checkbox">{{item.id}} {{item.name}}
    25. </p>
    26. </div>
    27. <script>
    28. var vm = new Vue({
    29. el: '#app',
    30. data: {
    31. id:'',
    32. name:'',
    33. list: [
    34. { id: 1, name: "曹操" },
    35. { id: 2, name: "吕布" },
    36. { id: 3, name: "诸葛亮" },
    37. { id: 4, name: "夏侯惇" },
    38. { id: 5, name: "吕伟" },
    39. { id: 6, name: "夏侯渊" },
    40. ]
    41. },
    42. methods: {
    43. add(){
    44. this.list.push({id:this.id,name:this.name})
    45. }
    46. }
    47. });
    48. </script>
    49. </body>
    50. </html>

    1.gif