TodoHeader : ์ ํ๋ฆฌ์ผ์ด์
์ด๋ฆ ํ์
TodoInput : ํ ์ผ ์
๋ ฅ ๋ฐ ์ถ๊ฐ
TodoList : ํ ์ผ ๋ชฉ๋ก ํ์ ๋ฐ ํน์ ํ ์ผ ์ญ์
TodoFooter: ํ ์ผ ๋ชจ๋ ์ญ์
<template>
<header>
<h1>TODO it!</h1>
</header>
</template>
<script>
export default{
}
</script>
<style>
h1{
color: #2f3b52;
font-weight: 900;
margin: 2.5rem 0 1.5rem;
}
</style>
<template>
<div class="inputBox shadow">
<!--
* v-model
- ํผ์์ ์ฃผ๋ก ์ฌ์ฉ๋๋ ์์ฑ, ํผ์ ์
๋ ฅํ ๊ฐ์ ๋ทฐ ์ธ์คํด์ค์ ๋ฐ์ดํฐ์ ์ฆ์ ๋๊ธฐํ ์์ผ์ค๋ค.
- ํ๋ฉด์ ์
๋ ฅ๋ ๊ฐ์ ์ ์ฅํ์ฌ ์๋ฒ์ ๋ณด๋ด๊ฑฐ๋ ์ถ๊ฐ ๋ก์ง์ ์ํ ํ ์ ์๋ค.
-->
<input type="text" v-model= "newTodoItem">
<button>์ถ๊ฐ</button>
</div>
</template>
<script>
export default{
data(){
return{
newTodoItem: ''
}
},
}
</script>
...
<template>
<div class="inputBox shadow">
<input type="text" v-model= "newTodoItem">
<button v-on:click = "addTodo">์ถ๊ฐ</button>
</div>
</template>
<script>
export default{
data(){
return{
newTodoItem: ''
}
},
methods : {
addTodo(){
console.log(this.newTodoItem);
}
}
}
</script>
...
<template>
<div class="inputBox shadow">
<input type="text" v-model= "newTodoItem">
<button v-on:click = "addTodo">์ถ๊ฐ</button>
</div>
</template>
<script>
export default{
data(){
return{
newTodoItem: ''
}
},
methods: {
addTodo(){
localStorage.setItem(this.newTodoItem, this.newTodoItem);
}
}
}
</script>
...
<template>
<div class="inputBox shadow">
<input type="text" v-model= "newTodoItem">
<button v-on:click = "addTodo">์ถ๊ฐ</button>
</div>
</template>
<script>
export default{
data(){
return{
newTodoItem: ''
}
},
methods: {
addTodo(){
// 1. ์ธํ ๋ฐ์ค์ ์
๋ ฅ ๊ฐ์ด ์์ ๋๋ง ์ ์ฅ
if(this.newTodoItem !== ""){
// 2. ์ธํ๋ฐ์ค์ ์
๋ ฅ๋ ํ
์คํธ์ ์๋ค ๊ณต๋ฐฑ ๋ฌธ์์ด ์ ๊ฑฐ
var value = this.newTodoItem && this.newTodoItem.trim();
localStorage.setItem(value, value);
//3. ์ธํ ๋ฐ์ค์ ์
๋ ฅ ๊ฐ ์ด๊ธฐํ
this.clearInput();
}
},
clearInput() {
this.newTodoItem = '';
}
}
}
</script>
...
<template>
<div class="inputBox shadow">
<input type="text" v-model= "newTodoItem" placeholder="type what your have to do" @keyup.enter= "addTodo">
<span class="addContainer" v-on:click = "addTodo">
<i class="addBtn fa fa-plus"></i>
</span>
</div>
</template>
<script>
export default{
data(){
return{
newTodoItem: ''
}
},
methods: {
addTodo(){
// 1. ์ธํ ๋ฐ์ค์ ์
๋ ฅ ๊ฐ์ด ์์ ๋๋ง ์ ์ฅ
if(this.newTodoItem !== ""){
// 2. ์ธํ๋ฐ์ค์ ์
๋ ฅ๋ ํ
์คํธ์ ์๋ค ๊ณต๋ฐฑ ๋ฌธ์์ด ์ ๊ฑฐ
var value = this.newTodoItem && this.newTodoItem.trim();
localStorage.setItem(this.newTodoItem, this.newTodoItem);
//3. ์ธํ ๋ฐ์ค์ ์
๋ ฅ ๊ฐ ์ด๊ธฐํ
this.clearInput();
}
},
clearInput() {
this.newTodoItem = '';
}
}
}
</script>
<style>
input:focus {
outline: none;
}
.inputBox {
background: #fff;
height: 50px;
line-height: 50px;
border-radius: 5px;
}
.inputBox input {
border-style: none;
font-size: 0.9rem;
}
.addContainer {
float:right;
background: linear-gradient(to right, #6478fb, #8763fb);
display: block;
width: 3rem;
border-radius: 0 5px 5px 0;
}
.addBtn{
color: #fff;
vertical-align: middle;
}
</style>
...
<script>
export default{
//1. todoItems ๋ฐ์ดํฐ ์์ฑ์ ๋น ๋ฐฐ์ด๋ก ์ ์ธํ๋ค.
data() {
return {
todoItems : []
}
},
// 2. created ๋ผ์ดํ์ฌ์ดํด ํ
์ for ๋ฐ๋ณต๋ฌธ๊ณผ push()๋ก ๋ก์ปฌ ์คํ ๋ฆฌ์ง์ ๋ฐ์ดํฐ๋ฅผ todoItems์ ๋ด์์ค๋ค.
// created
// - ์ธ์คํด์ค๊ฐ ํ๋ฉด์ ๋ถ์ฐฉ๋๊ธฐ ์ ๋จ๊ณ
// - data์์ฑ๊ณผ methods์์ฑ์ ์ ๊ทผํ ์ ์๋ ์ฒซ ๋ผ์ดํ ์ฌ์ดํด ๋จ๊ณ
// - ์ปดํฌ๋ํธ๊ฐ ์์ฑ๋๊ณ ๋์ ์คํ๋๋ ๋จ๊ณ๋ก, ์๋ฒ์ ๋ฐ์ดํฐ๋ฅผ ์์ฒญํ์ฌ ๋ฐ์์ค๋ ๋ก์ง์ ์ํํ๋ค.
created() {
if(localStorage.length > 0){
for(var i = 0, i < localStorage.length; i++){
this.todoItems.push(localStorage.key(i));
}
}
}
}
</script>
...
<template>
<section>
<ul>
<!--
v-for = "์์ดํ
๋ช
in array"
: ์ง์ ํ ๋ฐ์ดํฐ์ ๊ฐฏ์๋งํผ html์ ์ถ๋ ฅ
-->
<li v-for= "todoItem in todoItems">{{ todoItem }}</li>
</ul>
</section>
</templat>
v-for
๋ ๋ทฐ ๋ฐ์ดํฐ ์์ฑ todoItems์ ๋ด์ฉ๋ฌผ ๊ฐ์๋งํผ ๋ฐ๋ณตํด์ <li>
ํ๊ทธ๋ฅผ ์ถ๋ ฅํด ์ค๋ค. โ ๋ฌธ์ ์ : ํ ์ผ์ ์ถ๊ฐํด๋ ํ๋ฉด์ด ๋ฐ๋ก ๊ฐฑ์ ๋์ง ์๊ณ ๋ธ๋ผ์ฐ์ ์๋ก๊ณ ์นจ์ ํด์ผํ๋ค.
<template>
<section>
<ul>
<li v-for= v-for= "todoItem in todoItems" :key="todoItem" class="shadow">
<i class="checkBtn fa fa-check"></i>
{{ todoItem }}
<span class="removeBtn" type="button">
<i class="fa fa-trash"></i>
</span>
</li>
</ul>
</section>
</template>
...
<style>
ul{
list-style-type: none;
padding-left: 0px;
margin-top: 0;
text-align: left;
}
li{
display: flex;
min-height: 50px;
height: 50px;
line-height: 50px;
margin: 0.5rem 0;
padding: 0 0.9rem;
background: #fff;
border-radius: 5px;
}
.checkBtn{
line-height: 45px;
color: #62acde;
margin-right:5px;
cursor: pointer;
}
.removeBtn{
line-height: 45px;
margin-left: auto;
color: #de4343;
cursor: pointer;
}
</style>
<span>
ํ๊ทธ์ ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ ์ถ๊ฐํ๋ค.<template>
<section>
<ul>
<li v-for= "todoItem in todoItems" :key="todoItem" class="shadow">
<i class="checkBtn fa fa-check"></i>
{{ todoItem }}
<span class="removeBtn" type="button" v-on:click = "removeTodo">
<i class="fa fa-trash"></i>
</span>
</li>
</ul>
</section>
</template>
<script>
export default{
...
methods: {
removeTodo() {
console.log('clicked');
}
}
}
</script>
...
v-for
๋๋ ํฐ๋ธ์์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณตํ๋ ๋ณ์์ด๋ค.v-for
๋๋ ํฐ๋ธ๋ก ๋ฐ๋ณตํ ์์๋ ๋ชจ๋ ๋ทฐ์์ ๋ด๋ถ์ ์ผ๋ก ์ธ๋ฑ์ค๋ฅผ ๋ถ์ฌํ๋ค.<template>
<section>
<ul>
<li v-for= "(todoItem, index) in todoItems" :key="todoItem" class="shadow">
<i class="checkBtn fa fa-check"></i>
{{ todoItem }}
<span class="removeBtn" type="button" v-on:click = "removeTodo(todoItem, index)">
<i class="fa fa-trash"></i>
</span>
</li>
</ul>
</section>
</template>
<script>
export default{
...
methods: {
removeTodo(todoItem, index) {
console.log(todoItem, index);
}
}
}
</script>
...
<script>
export default{
...
methods: {
removeTodo(todoItem, index) {
localStorage.removeItem(todoItem);
this.todoItems.splice(index, 1); // splice() : ๋ฐฐ์ด์ ํน์ ์ธ๋ฑ์ค์์ ๋ถ์ฌํ ์ซ์๋งํผ์ ์ธ๋ฑ์ค๋ฅผ ์ญ์ ํ๋ค.
}
}
}
</script>
removeItem()
API๋ todoItem ์ธ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ก์ปฌ ์คํ ๋ฆฌ์ง์์ ํ ์ผ ํ
์คํธ๋ฅผ ์ญ์ ํ๋ค.splice()
API๋ ์ธ์๋ก ๋ฐ์ index๋ฅผ ์ด์ฉํ์ฌ ๋ฐฐ์ด์ ํด๋น ์ธ๋ฑ์ค์์ 1๋งํผ ์ญ์ ํ๋ค.<template>
<div class="clearAllContainer">
<span class="clearAllBtn" v-on:click = "clearTodo">Clear All</span>
</div>
</template>
<script>
export default{
methods: {
clearTodo(){
localStorage.clear(); //๋ก์ปฌ ์คํ ๋ฆฌ์ง์ ๋ฐ์ดํฐ๋ฅผ ๋ชจ๋ ์ญ์
}
}
}
</script>
<style>
.clearAllContainer{
width: 8.5rem;
height: 50px;
line-height: 50px;
background: #fff;
border-radius: 5px;
margin: 40px auto 0;
}
.clearAllBtn{
display: block;
color: #e20303;
cursor: pointer;
}
</style>
โ ๋ฌธ์ ์ : Clear All ๋ฒํผ์ ํด๋ฆญํ๋ฉด ๋ธ๋ผ์ฐ์ ๋ฅผ ์๋ก ๊ณ ์นจํด์ผ๋ง ๋ก์ปฌ ์คํ ๋ฆฌ์ง์ ๋ฐ์ดํฐ๊ฐ ๋ฐ์๋๋ค