Vue官网todoMVC示例代码

2020-06-16 06:06:10易采站长站整理

清除全部已完成todos


<div class="data_clearTodos"
@click="clearTodos"
v-show="times < todoLists.length"> //如果待办事件次数小于总todoLists长度就显示“clear completed”
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >clear completed</a>
</div>

<div class="data_clearTodos"
@click="clearTodos"
v-show="times === todoLists.length"> //如果待办事件次数等于总todoLists长度就显示“clear completed”
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></a>
</div>


clearTodos() { //清空已完成的todoLists,使用filter的api进行筛选
this.todoLists = this.todoLists.filter(todo => todo.isChecked === false)
window.localStorage.setItem("content",JSON.stringify(this.todoLists)) //以json格式存储数据
},

如果待办todos的times小于todoLists长度,就证明有已完成的todo,就可以显示“clear completed”,如果相等就说明没有已完成的todo。

三种状态筛选

所有todos,已完成todos,未完成todos筛选


<li class="content_todoList"
v-show="defaultShow || (whichShow?list.isChecked:!list.isChecked)">

<div class="data_status">
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow"
:class="{active: index === dataStatusIndex}" //动态class实现tab切换
v-for="(item ,index) in dataStatus" v-for循环
@click="switchStatus(index)" //切换不同tab显示内容
:key="index">
{{item}}
</a>
</div>


switchStatus(index) { //通过if条件判断操作
this.dataStatusIndex = index
if (this.dataStatus[index] === "Active") {
this.defaultShow = false
this.whichShow = false
} else if (this.dataStatus[index] === "Completed") {
this.defaultShow = false
this.whichShow = true
} else if (this.dataStatus[index] === "All") {
this.defaultShow = true
}
},

我这里是同时if条件句判断操作,有点麻烦,目前还没有想出来其他方案。在li元素使用三元运算符和或运算符进行操作显示不同状态的todos。

完整代码


<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}

input {
outline: none;
}

ul,