任務清單可以極大提高我們的工作效率、哪些事情辦了、哪些事情代辦、哪些是緊急需要辦的事情等等。
在元件化編碼實戰3的基礎上進一步改進、将原先的資料儲存的浏覽器中。就可以做到關閉網頁也不會丢失資料的情況
文章目錄
- 1、功能描述
- 2、效果示範
- 3、在實戰三代碼上修改(核心部分代碼、将資料儲存到浏覽器)
- 4、已經打包好的項目(包含項目源碼)
- 5、友情提示
1、功能描述
- 1、可以添加當天的任務到任務清單
- 2、可以勾選已經完成的項目
- 3、統計任務數量以及完成數量
- 4、删除已完成的任務
- 5、可以檢視當天的任務清單(關閉網頁重新打開同樣可以看到)
2、效果示範
3、在實戰三代碼上修改(核心部分代碼、将資料儲存到浏覽器)
<template>
<div id="root">
<div class="todo-container">
<div class="todo-wrap">
<TheHeader :addTodo="addTodo" />
<TheList
:todos="todos"
:checkTodo="checkTodo"
:deleteTodo="deleteTodo"
/>
<TheFooter
:todos="todos"
:checkAllTodo="checkAllTodo"
:clearAllTodo="clearAllTodo"
/>
</div>
</div>
</div>
</template>
<script>import TheHeader from "./components/TheHeader";
import TheList from "./components/TheList";
import TheFooter from "./components/TheFooter.vue";
export default {
name: "App",
components: { TheHeader, TheList, TheFooter },
data() {
return {
//由于todos是MyHeader元件和MyFooter元件都在使用,是以放在App中(狀态提升)
todos: JSON.parse(localStorage.getItem("todos")) || [],
// todos: [
// { id: "001", title: "吃飯", done: true },
// { id: "002", title: "睡覺", done: false },
// { id: "003", title: "打豆豆", done: true },
// ],
};
},
methods: {
//添加一個todo
addTodo(todoObj) {
this.todos.unshift(todoObj);
},
//勾選or取消勾選一個todo
checkTodo(id) {
this.todos.forEach((todo) => {
if (todo.id === id) todo.done = !todo.done;
});
},
//删除一個todo
deleteTodo(id) {
this.todos = this.todos.filter((todo) => todo.id !== id);
},
//全選or取消全選
checkAllTodo(done) {
this.todos.forEach((todo) => {
todo.done = done;
});
},
//清除所有已經完成的todo
clearAllTodo() {
this.todos = this.todos.filter((todo) => {
return !todo.done;
});
},
},
watch: {
todos: {
deep: true,
handler(value) {
localStorage.setItem("todos", JSON.stringify(value));
},
},
},
};</script>
<style>/*base*/
body {
background: #fff;
}
.btn {
display: inline-block;
padding: 4px 12px;
margin-bottom: 0;
font-size: 14px;
line-height: 20px;
text-align: center;
vertical-align: middle;
cursor: pointer;
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
0 1px 2px rgba(0, 0, 0, 0.05);
border-radius: 4px;
}
.btn-danger {
color: #fff;
background-color: #da4f49;
border: 1px solid #bd362f;
}
.btn-danger:hover {
color: #fff;
background-color: #bd362f;
}
.btn:focus {
outline: none;
}
.todo-container {
width: 600px;
margin: 0 auto;
}
.todo-container .todo-wrap {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}</style>