实现了增、删功能,任务统计,全删除功能。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<body>
<div id="app">
<input placeholder="请输入待执行的任务" v-model="todoName">
<button @click="add">添加任务</button>
<ul>
<li v-for="(item,index) in list" :key="item.id">
{{index+1}} {{item.name}}
<button @click="del(item.id)">删除</button>
</li>
</ul>
<div>
合计:{{list.length}}
<button @click="clear">清空任务</button>
</div>
</div>
<script>
const app = new Vue({
el: "#app",
data: {
todoName: '',
list: [
{ id: 1, name: '跑步一公里' }
]
},
methods: {
del(id) {
confirm('是否删除该任务?')
this.list = this.list.filter(item => item.id !== id)
},
add() {
if (this.todoName.trim() == '') {
alert('请输入有效内容')
return
}
this.list.unshift({
id: +new Date(),
name: this.todoName
})
this.todoName = ''
},
clear() {
this.list = []
}
}
})
</script>
</body>
</html>