Skip to content

Commit 4d4786a

Browse files
committed
feat: manage TodoItems in App.vue
1 parent 5481933 commit 4d4786a

File tree

5 files changed

+39
-79
lines changed

5 files changed

+39
-79
lines changed

โ€Ždoit/todo/src/App.vue

+30-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<template>
22
<TodoHeader />
3-
<TodoInput />
4-
<TodoList />
5-
<TodoFooter />
3+
<TodoInput @addTodo="addTodo" />
4+
<TodoList :todoItems="todoItems" @removeTodo="removeTodo" />
5+
<TodoFooter @removeAll="clearAll" />
66
</template>
77

88
<script>
@@ -19,6 +19,33 @@ export default {
1919
TodoList,
2020
TodoFooter,
2121
},
22+
data() {
23+
return {
24+
todoItems: [],
25+
};
26+
},
27+
methods: {
28+
clearAll() {
29+
localStorage.clear();
30+
this.todoItems = [];
31+
},
32+
addTodo(newTodoItem) {
33+
localStorage.setItem(newTodoItem, newTodoItem);
34+
this.todoItems.push(newTodoItem);
35+
},
36+
removeTodo(todoItem, index) {
37+
localStorage.removeItem(todoItem);
38+
this.todoItems.splice(index, 1);
39+
},
40+
},
41+
created() {
42+
// ๋ทฐ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜์ž๋งˆ์ž ๋ทฐ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก created()์—์„œ ๋กœ์ปฌ์Šคํ† ๋ฆฌ์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ทฐ ๋ฐ์ดํ„ฐ๋กœ ์˜ฎ๊น€
43+
if (localStorage.length > 0) {
44+
for (let i = 0; i < localStorage.length; i++) {
45+
this.todoItems.push(localStorage.key(i));
46+
}
47+
}
48+
},
2249
};
2350
</script>
2451

โ€Ždoit/todo/src/components/HelloWorld.vue

-58
This file was deleted.

โ€Ždoit/todo/src/components/TodoFooter.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
export default {
99
methods: {
1010
clearTodo() {
11-
localStorage.clear();
11+
this.$emit("removeAll");
1212
},
1313
},
1414
};

โ€Ždoit/todo/src/components/TodoInput.vue

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
type="text"
55
v-model="newTodoItem"
66
placeholder="Things I have to do..."
7-
@keyup.enter="addTodo"
7+
@keypress.enter="addTodo"
88
/>
99
<span class="addContainer" @click="addTodo">
1010
<i class="addBtn fas fa-plus" aria-hidden="true"></i>
@@ -23,13 +23,17 @@ export default {
2323
addTodo() {
2424
if (this.newTodoItem !== "") {
2525
const value = this.newTodoItem && this.newTodoItem.trim();
26-
localStorage.setItem(value, value);
26+
this.$emit("addTodo", value);
2727
this.clearInput();
2828
}
2929
},
3030
clearInput() {
3131
this.newTodoItem = "";
3232
},
33+
removeTodo(todoItem, index) {
34+
localStorage.removeItem(todoItem);
35+
this.todoItems.splice(index, 1);
36+
},
3337
},
3438
};
3539
</script>

โ€Ždoit/todo/src/components/TodoList.vue

+2-15
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,10 @@
1818

1919
<script>
2020
export default {
21-
data() {
22-
return {
23-
todoItems: [],
24-
};
25-
},
26-
created() {
27-
// ๋ทฐ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜์ž๋งˆ์ž ๋ทฐ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก created()์—์„œ ๋กœ์ปฌ์Šคํ† ๋ฆฌ์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ทฐ ๋ฐ์ดํ„ฐ๋กœ ์˜ฎ๊น€
28-
if (localStorage.length > 0) {
29-
for (let i = 0; i < localStorage.length; i++) {
30-
this.todoItems.push(localStorage.key(i));
31-
}
32-
}
33-
},
21+
props: ["todoItems"],
3422
methods: {
3523
removeTodo(todoItem, index) {
36-
localStorage.removeItem(todoItem);
37-
this.todoItems.splice(index, 1);
24+
this.$emit("removeTodo", todoItem, index);
3825
},
3926
},
4027
};

0 commit comments

Comments
ย (0)