Vue:实现一个基本的项目todolist
app.js代码:
(function(Vue){
new Vue({
el:'#todoapp',
data:{
title:'Todoapp',
list:[]
},
methods:{
add:function(ev){
var input = ev.target;
value=input.value.trim();
if(value == ""){
return;
}
data ={
id:this.list.length+1,
name:value,
status:false,
shows:true,
}
this.list.push(data);
input.value = '';
},
selectAll:function(ev){
var input = ev.target;
for(let i=0;i<this.list.length;i++){
this.list[i].status = input.checked;
}
},
del:function(key){
this.list.splice(key,1)
},
delCompleted:function(){
this.list = this.list.filter((v)=> v.status == false)
},
}
})
})(Vue)
index.html代码
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Template • TodoMVC</title>
<link rel="stylesheet" href="node_modules/todomvc-common/base.css">
<link rel="stylesheet" href="node_modules/todomvc-app-css/index.css">
<!-- CSS overrides - remove if you don't need it -->
<link rel="stylesheet" href="css/app.css">
</head>
<body >
<section class="todoapp" id="todoapp">
<header class="header">
<h1>{{title}}</h1>
<input class="new-todo" @keyup.enter="add($event)" placeholder="What needs to be done?" autofocus>
</header>
<div v-if="list.length">
<!-- This section should be hidden by default and shown when there are todos -->
<section class="main">
<input id="toggle-all" @click="selectAll($event)" class="toggle-all" type="checkbox">
<label for="toggle-all">选中全部</label>
<ul class="todo-list">
<!-- These are here just to show the structure of the list items -->
<!-- List items should get the class `editing` when editing and `completed` when marked as completed -->
<li v-for="(val,key) in list" v-if="val.shows" v-bind:class="{completed:val.status}">
<div class="view">
<input class="toggle" type="checkbox" v-model="val.status">
<label >{{val.name}}</label>
<button @click="del(key)" class="destroy"></button>
</div>
<input class="edit" value="Create a TodoMVC template">
</li>
</ul>
</section>
<!-- This footer should be hidden by default and shown when there are todos -->
<footer class="footer">
<!-- This should be `0 items left` by default -->
<span class="todo-count"><strong>{{list.length}}</strong> item left</span>
<!-- Remove this if you don't implement routing -->
<ul class="filters">
<li>
<a class="selected" href="#/" @click="all()" >All</a>
</li>
<li>
<a href="#/active" @click="active()">Active</a>
</li>
<li>
<a href="#/completed" @click="completed()" >Completed</a>
</li>
</ul>
<!-- Hidden if no completed items are left ↓ -->
<button @click="delCompleted()" class="clear-completed">Clear completed</button>
</footer>
</section>
</div>
<footer class="info">
<p>双击修改</p>
<!-- Remove the below line ↓ -->
<p>Template by <a href="http://sindresorhus.com">Sindre Sorhus</a></p>
<!-- Change this out with your name and url ↓ -->
<p>Created by <a href="http://todomvc.com">you</a></p>
<p>Part of <a href="http://todomvc.com">TodoMVC</a></p>
</footer>
<!-- Scripts here. Don't remove ↓ -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<script src="node_modules/todomvc-common/base.js"></script>
<script src="js/app.js"></script>
</body>
</html>