vue.js 第一个Demo 增删便签
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<link rel="stylesheet" type="text/css" href="app.css">
</head>
<body>
<div class="main">
<h3 class="big-title">添加任务:</h3>
<input placeholder="按回车添加" class="task-input" type="text"
@keyup.enter="addTodo"
v-model="todo"
>
<ul class="task-count">
<li>{{unfinishedCount}}个任务未完成</li>
<li class="action">
<a class="active" href="#all">所有任务</a>
<a href="#unfinished">未完成的任务</a>
<a href="#finished">完成的任务</a>
</li>
</ul>
<h3 class="big-title">任务列表:</h3>
<div class="tasks">
<span class="no-task-tip" v-show="!list.length">还没有添加任何任务</span>
<ul class="todo-list">
<li class="todo" :class="{completed:item.isChecked,editing:item===edtorTodos}" v-for="item in filteredList">
<div class="view">
<input checked class="toggle" type="checkbox" v-model="item.isChecked">
<label @dblclick="edtorTodo(item)">{{item.title}}</label>
<button class="destroy" @click="deleteTodo(item)">删除</button>
</div>
<input
v-foucs="edtorTodos===item"
class="edit"
type="text"
v-model="item.title"
@blur="edtorTodoed(item)"
@keyup.13="edtorTodoed(item)"
@keyup.esc="cancleTodo(item)"
>
</li>
</ul>
</div>
</div>
</body>
<script src='./app.js'></script><!--注意把js放在后面,要不然找不到类main-->
</html>
js:
//存取保存的数据
var store = {
save(key,value){
localStorage.setItem(key,JSON.stringify(value));
},
fetch(key){
return JSON.parse(localStorage.getItem(key)) || [];
}
}
//取出store对象中key值为mylist的数据
var list = store.fetch("mylist");
//MVVM中的VM对象
var vm = new Vue({
el:".main",
data:{
list:list,
todo:'',
edtorTodos:'',
originalTitle:'',
visibility:"all"//通过这个属性值的变化对数据进行筛选
},
methods:{
//添加数据
addTodo(){
this.list.push({
title:this.todo,
isChecked:false,
});
this.todo='';
},
//删除数据
deleteTodo(todo){
var index = this.list.indexOf(todo);
this.list.splice(index,1);
},
//编辑数据
edtorTodo(todo){
this.originalTitle=todo.title;
this.edtorTodos=todo;
},
//编辑完成
edtorTodoed(todo){
this.edtorTodos='';
},
//取消编辑
cancleTodo(todo){
todo.title=this.originalTitle;
this.edtorTodos='';
}
},
directives:{
"foucs":{
update(el,binding){
if(binding.value){
el.focus();
}
}
}
},
//计算属性
computed:{
unfinishedCount:function(){
//统计list中isChecked为false的项的个数
return this.list.filter(function(item){return !item.isChecked}).length;
},
//根据hash过滤后的数据
filteredList:function(){
var filter = {
all:function(list){
return list;
},
finished:function(list){
return list.filter(function(item){
return item.isChecked;
})
},
unfinished:function(list){
return list.filter(function(item){
return !item.isChecked;
})
}
};
return filter[this.visibility](list);
}
},
//监控,当list改变时,调用store的save方法
watch:{
list:{
handler:function(){
store.save("mylist",this.list);
},
deep:true //深层监控,保证存储到list对象中每一层如isChecked
}
}
});
function watchHashChange(){
var hash = window.location.hash.slice(1);
vm.visibility = hash;
console.log(hash);
}
watchHashChange();
window.addEventListener("hashchange",watchHashChange);
css
.big-title{
background-color:goldenrod;
}
.completed{
text-decoration:line-through
}
.todo > :not(:first-child) {
display: none;
}
.editing > :first-child{
display: none;
}
.editing > :not(:first-child) {
display: block;
}