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
| <div id="app"> <input type="text" v-model="inputValue"> <button @click='handleBtnClick'>提交</button> <ul> <todo-item :content='item' :index='index' v-for="(item,index) in list" @delete='handleItemDelete'> </todo-item> </ul> </div> <script> // template:"<li>{{content}}</li>"
let TodoItem = { props:['content','index'], template:"<li @click='handleItemClick'>{{content}}</li>" , // 组件模板 methods:{ handleItemClick:function(){ this.$emit('delete',this.index) } } }
let app = new Vue({ el: '#app', components:{ TodoItem }, data: { list: [], inputValue: '' }, methods: { handleBtnClick:function(){ this.list.push(this.inputValue) this.inputValue = '' }, handleItemDelete:function(index){ this.list.splice(index,1) }, } }) </script>
|