Vue小例子笔记(todolist)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>todoList</title>
		<script src="vue/vue.js"></script>
	</head>
	<body>
		<div id="root">
			
			<div>
				<input v-model="inputValue"/>
				<button @click="handleClick">提交</button>
			</div>
			<!--组件-->
			<ul>
				<todo-item v-for="(item,index) of list" :key="index" :content="item" :index="index" @delete="handleDelete"></todo-item>
				<!--父组件向子组件传值是通过属性进行传值的  @delete监听子组件事件delete-->
			</ul>
			
		</div>
		<script>
			
			/*
			 * 组件也是一个Vue实例
			 */
			
			
			//创建组件(全局)
			Vue.component('todo-item',{
				props:['content','index'],//接收到组件传递过来的数据
				template:'<li @click="handleClick">{{content}}</li>',
				methods:{
					handleClick:function(){
						this.$emit('delete',this.index)//向外触发一个事件delete 传递参数index
					}
				}
			})
			
			
			/**
			 *局部(在父组件里面无法调用该组件)
			 * 需要在该父组件中定义components
			 */
//			var todoItem={
//				template:'<li>item</li>'
//			}
			
			
			new Vue({
				el:"#root",
//				components:{//引入局部组件
//					'todo-item':todoItem
//				},
				data:{
					inputValue:'',
					list:[]
				},
				methods:{
					handleClick:function(){
						this.list.push(this.inputValue)
						this.inputValue=""
					},
					handleDelete:function(index){
						this.list.splice(index,1)//删除
					}
				}
			})
		</script>
	</body>
</html>

欢迎访问我的个人博客

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海面有风

您的鼓励将是我前进的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值