<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>hello-vue</title> </head> <body> <div id="app"> 姓: <input type="text" name="" id="" value="" v-model="firstName" /> <hr /> 名: <input type="text" name="" id="" value="" v-model="lastName" /> <hr /> 結果:{{firstName + lastName}} <hr /> <h1>管理食物</h1> <div class=""> <span>添加食物:</span> <input @keyup.enter="add" v-model="text" placeholder="What food?" autofocus="autofocus"/> </div> <ul> <li v-for="food in foods"> <input type="checkbox" name="" id="" value="" v-model="food.complate"/> {{ food.text }} <button @click="destroy(food.text)">刪除</button> </li> </ul> </div> <script src="js/vue.js" type="text/javascript" charset="utf-8"></script> <!--引進vue.js,當前環境就會多出一個Vue全局變量--> <script type="text/javascript"> (function(window){ foods = [{text:'辣條',complate:true},{text:'香蕉',complate:false},{text:'蘋果',complate:true}]; var app = new Vue({ el: '#app', data: { firstName: 'hu', lastName: 'sheng', foods: foods, text:'' }, methods:{ f:function(){ window.alert("aa"); }, add:function(){ if(this.text.trim().length === 0){ return } this.foods.push({ text:this.text, complate:false }); this.text = ''; }, destroy:function(text){ console.log(text); var foodIndex; this.foods.find(function(food,index){ if(food.text===text){ foodIndex = index; } }); this.foods.splice(foodIndex,1); } } }); })(window); </script> </body> <!-- 筆記: 1,v-on:能夠簡寫爲@ --> </html>