<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<title>Document</title>
</head>
<body>
<div id="root">
<input v-model="inputValue" type="text">
<button @click="addList">add</button>
<ul>
<todo-item
v-for="(item,index) of list"
:key="index"
:content="item"
:index="index"
@delete="deleteHandle"
/>
</ul>
</div>
<!-- vue -->
<script>
// 全局定义
Vue.component('todo-item',{
props:['content','index'],
template:'<li @click="deleteItem">{{content}}</li>',
methods:{
deleteItem(){
this.$emit('delete',this.index)
}
}
})
//局部定义
// let todoItem={
// template:'<li>{{item}}</li>'
// }
new Vue({
el:'#root',
// components:{
// 'todo-item':todoItem
// },
data:{
inputValue:"",
list:[]
},
methods:{
addList(){
this.list.push(this.inputValue)
this.inputValue=''
},
deleteHandle(idx){
this.list.splice(idx,1);
}
}
})
</script>
</body>
</html>
网友评论