vue.js - 我如何让按钮隐藏未完成的任务
问题描述
这是 Jsfiddle:https ://jsfiddle.net/zxo35mts/1/
本质上,我试图让按钮在单击时隐藏所有未完成的任务,并在再次单击时再次显示它们,但我不知道该怎么做
<div id="root">
<h1>
All Tasks
</h1>
<ul>
<li v-for="task in tasks" v-text="task.description"></li>
</ul>
<button @click="hideIncompleteTasks">show only completed</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<script>
new Vue({
el: "#root",
data: {
tasks: [
{ description: "go to the store", completed: true },
{ description: "finish screencast", completed: false },
{ description: "make donation", completed: false },
{ description: "clear inbox", completed: false },
{ description: "make dinner ", completed: false },
{ description: "clean room", completed: true },
]
},
methods: {
hideIncompleteTasks() {
if (!this.tasks.completed) {
}
}
},
})
</script>
解决方案
添加另一个showCompleted
可以通过按钮单击事件更新的属性,然后添加另一个shownTasks
基于第一个属性调用的计算属性:
new Vue({
el: "#root",
data: {
tasks: [
{ description: "go to the store", completed: true },
{ description: "finish screencast", completed: false },
{ description: "make donation", completed: false },
{ description: "clear inbox", completed: false },
{ description: "make dinner ", completed: false },
{ description: "clean room", completed: true },
],
showCompleted:false
},
computed:{
shownTasks(){ return this.showCompleted?this.tasks.filter(task=>task.completed):this.tasks;}
},
methods: {
hideIncompleteTasks() {
this.showCompleted=!this.showCompleted
}
},
})
然后渲染shownTasks
类似:
<ul>
<li v-for="task in shownTasks" v-text="task.description"></li>
</ul>
<button @click="hideIncompleteTasks">show only completed</button>
推荐阅读
- html - 我在 youtube iframe 之后有一个标题,而标题在 iframe 的右侧,为什么?
- asp.net-core - 使用 Identity Server 4 和 ASP.NET Core 和 Angular8 SPA 进行 Windows 身份验证
- android - 如何使用改造2获取数据对象
- sql - 在表格中找不到字段
- javascript - 如何找到 Power BI 数据选定事件的定义
- laravel - 未应用 created_at 日期字段的 Laravel 演员表
- swift - 使用游标递归函数从 CloudKit 获取所有记录 - 如何将结果传递到其范围之外?
- php - Shopify SDK - 导致内存泄漏的分页请求
- jupyter-notebook - Google Colab 磁盘空间已满
- rust - 将大浮点数转换为 int 时会发生什么?