1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>v-for</title> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> </meta> </meta> </head> <body> <div id="app"> <div v-for="school,index in schools"> <h2>{{index}}</h2> <h2>{{school.name}}</h2> <p>{{school.address}}</p> </div> <div v-for="food in food"> <span>{{food.name}}</span> <span>{{food.price}}</span> </div> <br/> 添加食物 <input type="text" v-model="food.name"> </input> <input type="text" v-model="food.price"> </input> <button @click="addFood">添加</button> <br/>删除食物 <input type="text" v-model="food.name"> </input> <button @click="removeFood">删除</button> </div> <script> var app = new Vue({ el: '#app', data: { message: 'Hello Vue!', schools:[ {name:'北京大学',address:'北京市海淀区'}, {name:'清华大学',address:'北京市海淀区'}, {name:'北京大学',address:'北京市海淀区'}, {name:'北京大学',address:'北京市海淀区' }, ], food:[ {name:'苹果',price:10}, {name:'香蕉',price:20}, {name:'西瓜',price:30}, {name:'葡萄',price:40}, {name:'草莓',price:50}, {name:'火龙果',price:60}, {name:'苹果',price:10}, {name:'香蕉',price:20}, {name:'西瓜',price:30}, ] }, methods:{ addFood(){ this.food.push({ name:this.food.name, price:this.food.price }) }, removeFood(){ for(var i=0;i<this.food.length;i++){ if(this.food[i].name==this.food.name){ this.food.splice(i,1) } } } } }) </script> </body> </html>
|