代碼如下所示:
<div id="app"> <input v-model="inputValue"> <button @click="handleSumbit">提交</button> <todo-item v-for="(item,index) of list":key="index":content="item":index="index" @delete="handleDelete"></todo-item></div>
子組件向父組件傳遞數據使用 this.$emit('delete',this.index)
該方法
<script > Vue.component('todo-item',{ props:["content",'index'], template:"<li @click='handleSubmit'>{{content}}{{index}}</li>", methods:{ handleSubmit:function () { this.$emit('delete',this.index) } } }) new Vue({ el:'#app', data:{ inputValue:'', list:[] }, methods:{ handleSumbit:function () { this.list.push(this.inputValue); this.inputValue=""; }, handleDelete:function (index) { this.list.splice(index,1) } } })</script>
總結
以上所述是小編給大家介紹的Vue實現todolist刪除功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
新聞熱點
疑難解答