<!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">
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
</head>
<body>
<div id="root">
<div>
<input v-model="inputValue" />
<button @click="handSubmit">提交</button>
</div>
<ul>
<todo-item
v-for="(item,index) of list"
:key="index"
:content="item"
:index="index"
@delete ="handDelete"
>
</todo-item>
</ul>
</div>
<script>
Vue.component('todo-item', {
props: ['content', 'index'],//接收父组件的数据
template: '<li @click="handClick">{{content}}</li>',
methods: {
handClick: function() {
this.$emit('delete', this.index)//像父组件发送信号
}
}
})
// var TodoItem = {
// template: '<li>item</li>'
// }
new Vue({
el: "#root",
// components: {
// 'todo-item' :TodoItem
// },
data: {
inputValue: '',
list: []
},
methods: {
handSubmit: function(){
this.list.push(this.inputValue)
this.inputValue = ''
},
handDelete: function(index) {
this.list.splice(index, 1)
}
}
})
</script>
</body>
</html>
vue 的 todolist
最新推荐文章于 2022-11-13 16:36:41 发布