<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vue中组件与实例的关系</title>
<script src="./vue.js"></script>
</head>
<body>
<div id="root">
<div>
<input v-model="inputValue" />
<button @click="handleSubmit">提交</button>
</div>
<ul>
<todo-item v-for="(item,index) of list"
:key="index"
:content="item"
:index="index"
@click="handleDelete"
>
</todo-item>
</ul>
</div>
<script>
//全局组件 (vue中组件与实例的关系:vue的每一个组件都是vue的实例);
Vue.component('todo-item',{
props:['content','index'],//从副组件中接收content数据和index下标
template:'<li>{{content}} {{index}}</li>',
methods:{
handleClick:function(){
this.$emit('delete',this.index)
},
}
});
new Vue({
el:"#root",
data:{
inputValue:'',
list:[]
},
methods:{
handleSubmit:function () {
this.list.push(this.inputValue)
this.inputValue = ''
},
handleDelete:function () {
alert(11);
}
}
});
</script>
</body>
</html>