vue组件实现删除功能

vue组件实现删除功能

  • 目标
    当我在input框中输入数据后,点击提交按钮,保存
    点击我添加的内容进行删除
    在这里插入图片描述
    源代码
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="./vue.js"></script>
	</head>
	<body>
		<div id="root">
			<input type="text" v-model="inputValue">
			<button @click="handleSubmit">提交</button>
			<ui>
		
				<todo-item
				v-for="(item,index) of list"
				:key="index"
				:content="item"
				:index="index"
				@delete="handleDelete"></todo-item>
			</ui>
		</div>
		<script type="text/javascript">
			var TodoItem={
     
     
				props:['content','index'],
				template:'<li @click="handleClick">{
     
     {content}}</li>',
				methods:{
     
     
					handleClick:function(){
     
     
						this.$emit('delete',this.index) //$emit() 触发父组件的自定义事件
					}
				}
			}
			new Vue({
     
     
				el:"#root",
				components:{
     
     
					'todo-item':TodoItem
				},
				data:{
     
     
					inputValue:'',
					list:[],
				},
				methods:{
     
     
					handleSubmit:function(){
     
     
						this.list.push(this.inputValue);
						this.inputValue='';
					},
					handleDelete:function(index){
     
     
						this.list.splice(this.index,1);  //splice()方法 添加或删除数组中的元素。
					
					}
				}
			})
		</script>
	</body>
</html>

Guess you like

Origin blog.csdn.net/CSDN_java1005/article/details/113617616