🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
子组件自定义事件,向父组件传递参数 ~~~ this.$emit('delete',{}) ~~~ >[success]ge:删除对应的子组件 ~~~ <div id="app"> <input type="text" v-model="value"> <button @click="add">添加</button> <todo-item :content="item" :index="index" v-for="(item,index) of arr" @delete="handleDelete"></todo-item> </div> ~~~ ~~~ var TodoItem ={ props:['content','index'], template:"<div @click='handleClick'>{{content}}</div>", methods:{ handleClick(){ this.$emit('delete',this.index) } } } var app = new Vue({ el: "#app", components:{ TodoItem }, data:{ arr:[], value:'' }, methods:{ add(){ let repeat = this.arr.includes(this.value); if(this.value!=""&& !repeat){ this.arr.push(this.value) } }, handleDelete(index){ console.log(index) this.arr.splice(index,1); } } }) ~~~