1. Delete Todo ์ฝ๋ ์์ฑํ๊ธฐ
๋จผ์ ๋ฐ์ดํฐ๋ฅผ store์์ ๊ด๋ฆฌ ์ค์ด๋๊น actions์ mutations๋ฅผ ์์ฑํด์ผ ํ๋ค.
// src/store/index.js
mutations: {
mutationsAddTodo(state, todo) {
state.todos.push(todo);
console.log(todo);
},
mutationsDelTodo(state, id) {
state.todos = state.todos.filter((todo) => todo.id != id);
}
},
actions: {
actionsAddTodo({commit}, todo) {
commit('mutationsAddTodo', todo);
},
actionsDelTodo({commit}, id) {
commit('mutationsDelTodo', id);
}
},
actions์ actionsDelTodo
ํจ์๋ฅผ ์์ฑํ๊ณ , todo์ id๋ฅผ ๋ฐ๋๋ค.
mutations์ mutationsDelTodo
์์๋ ํด๋น id๊ฐ ์๋ ๊ฒ๋ง ๊ฐ์ ธ์ค๋ ํํฐ ํจ์๋ฅผ ์ง ๋ค.
// src/components/TodoItem.vue
export default {
props: {
todo: {}
},
methods: {
...mapActions(['actionsDelTodo'])
},
}
TodoItem์ mapActions
๋ฅผ ์์ฑํ์ฌ store actions
์ ์ฐ๊ฒฐํด์ฃผ๊ณ
<!-- src/components/TodoItem.vue -->
<v-btn @click="actionsDelTodo(todo.id)" block color="error">Delete</v-btn>
์์ฑํด๋ Delete ๋ฒํผ์ ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ ์ถ๊ฐํ๊ณ todo์ id๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ์ ์๋ํ๋ค !

2. Edit Todo ์ฝ๋ ์์ฑํ๊ธฐ
์ด์ Add์ Delete๊น์ง ๊ฐ๋ฅํ๋ Edit ๊ธฐ๋ฅ๋ง ๋ฃ์ผ๋ฉด
๊ธฐ๋ณธ์ ์ธ TodoList App ์์ฑ์ ๋์ด ๋๋ค !
// src/components/TodoItem.vue
<script>
import { mapActions } from 'vuex';
export default {
props: {
todo: {}
},
data() {
return {
editing: null
}
},
methods: {
...mapActions(['actionsDelTodo'])
},
}
</script>
TodoItem์ editing
์ด๋ผ๋ ๋ณ์๋ฅผ ํ๋ ์ ์ธํด๋๋ค.
์ ๊ฑธ๋ก EDIT๋ผ๋ ๋ฒํผ์ ๋๋ ์ ๋ ํธ์ง ๋ฐ์ค๋ฅผ ์ผ๊ณ ๋๋ ์ํ๋ฅผ ๊ด๋ฆฌํ ๊ฑฐ๋ค.
<!-- src/components/TodoItem.vue -->
<template>
<v-container fluid>
<v-row class="mb-3" justify="center" align="center">
<v-col cols="4">
<h3 v-if="!editing">{{ todo.title }}</h3>
<v-text-field
label="Edit"
outlined
dense
hide-details
v-else
></v-text-field>
</v-col>
<v-col cols="1">
<v-btn @click="editing = !editing" block large color="primary">
{{ editing ? 'Save' : 'Edit' }}
</v-btn>
</v-col>
<v-col cols="1">
<v-btn @click="actionsDelTodo(todo.id)" block large color="error">Delete</v-btn>
</v-col>
</v-row>
</v-container>
</template>
๊ทธ๋ฆฌ๊ณ v-if
์ v-else
๋ฅผ ์ฌ์ฉํ์ฌ todo.title์ ๋ณด์ฌ์ค ๊ฑด์ง,
ํธ์ง ํ ์คํธ ํ๋๋ฅผ ๋ณด์ฌ์ค ๊ฑด์ง ์์ฑํ๋ค.
๐จ outlined, dense, hide-details๋ v-text-field์ style ์์ฑ
v-btn
์ text๋ editing
์ด true์ผ ๋๋ Save, false์ผ ๋๋ Edit๊ฐ ๋จ๊ฒ ์์ฑํ๊ณ
ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ ๋ฌ์์ ๋๋ฅผ ๋๋ง๋ค true, false๊ฐ ๋ฐ๋๊ฒ ์์ฑํ๋ค.
๐จ ์ผํญ์ฐ์ฐ์ โ ์กฐ๊ฑด ? true : false
state์ ์ ์ฅํด๋๊ณ ์ญ์ , ์์ ์ ํ๋ ค๋ฉด ์ฌ์ฉ์ ์ ๋ ฅ๊ฐ์ ๋ฐ์์ผ ํ๋๋ฐ
// src/components/TodoItem.vue
data() {
return {
todoText: '',
editing: null
}
},
script ์์ data์ todoText๋ฅผ ์ ์ธํด๋๋ค.
<!-- src/components/TodoItem.vue -->
<v-text-field
v-else
v-model="todoText"
label="Edit"
outlined
dense
hide-details
></v-text-field>
์์์ ์ ์ธํ todoText
๋ฅผ ๊ฐ์ ํ์ผ ๋ด์ text field์ v-model
๋ก ์ฐ๊ฒฐ์์ผ๋๋ค.
๊ทธ๋ฌ๋ฉด ์
๋ ฅ๊ฐ์ด v-model
๋ก ์ฐ๊ฒฐํด๋ todoText
๋ก ๋ค์ด๊ฐ๊ฒ ๋๋ค.
<!-- src/components/TodoItem.vue -->
<v-col cols="1">
<v-btn @click="modTodo(todo)" block large color="primary">
{{ editing ? 'Save' : 'Edit' }}
</v-btn>
</v-col>
์๊น ๋ง๋ค์๋ Save / Edit button์ ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ modTodo
๋ผ๋ ํจ์์ todo๋ฅผ ๋ฃ์ ๊ฑธ๋ก ๋ณ๊ฒฝํ๋ค.
// src/components/TodoItem.vue
methods: {
...mapActions(['actionsDelTodo', 'actionsModTodo']),
modTodo(todo) {
this.editing = !this.editing;
if(this.editing) this.actionsModTodo(todo);
else todo.title = this.todoText;
}
},
๊ฐ์ ํ์ผ์ mapActions
์ actionsModTodo
๋ฅผ ์ถ๊ฐํ๊ณ ,
modTodo
์๋ editing์ true, false๋ก ๋ฐ๊ฟ์ฃผ๋ ๊ตฌ๋ฌธ๊ณผ
if(this.editing)
์ผ๋ก ๊ฐ์ ํ์ธํ๊ณ actions๋ก ๋์ด๊ฐ ๊ฑด์ง,
todo.title์ todoText๋ก๋ง ๋ฐ๊ฟ์ค ๊ฑด์ง ๋น๊ต ์์ฑํ๋ค.
// src/store/index.js
mutations: {
mutationsAddTodo(state, todo) {
state.todos.push(todo);
console.log(todo);
},
mutationsDelTodo(state, id) {
state.todos = state.todos.filter((todo) => todo.id != id);
},
mutationsModTodo(state, todo) {
let idx = state.todos.findIndex(x=>x.id == todo.id);
if(idx > -1) {
state.todos[idx] = todo;
}
}
},
actions: {
actionsAddTodo({commit}, todo) {
commit('mutationsAddTodo', todo);
},
actionsDelTodo({commit}, id) {
commit('mutationsDelTodo', id);
},
actionsModTodo({commit}, todo) {
commit('mutationsModTodo', todo);
}
},
store ๋ด์ actions์ mutations์ Mod๊ด๋ จ ํจ์๋ฅผ ์์ฑํ๋ค.
mutationsModTodo
์์๋ ๋ฐ์ todo์ ์์ด๋์ ๊ฐ์ ๊ฐ์ state์์ ์ฐพ๊ณ ,
๊ทธ Index์ ํด๋นํ๋ todo๋ฅผ ๋ฐ์ todo๋ก ๊ต์ฒดํด์ฃผ๋ฉด ์์ ์๋ฃ !
3. ๊ฒฐ๊ณผ

๋ค์์ ๋ญ ๋ง๋ค์ด๋ณผ๊น ๐ค
'Frontend > Vue.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Vue] Webpack ๋น๋ ์ ์ด๋ฏธ์ง ํด๋ ๊ฒฝ๋ก ๋ณ๊ฒฝ ๋ฐ ํ์ผ์ด ์๋ ๊ฒฝ์ฐ (0) | 2025.06.26 |
---|---|
[Vue.js] Vue2 vs Vue3 ๋ณํ๋ ์ (0) | 2022.07.20 |
[Vue.js - Vuex] Todo List ๋ง๋ค๊ธฐ โก (0) | 2022.06.01 |
[Vue.js - Vuex] Todo List ๋ง๋ค๊ธฐ โ (0) | 2022.06.01 |
[Vue.js - Vuetify] Vuetify ์ถ๊ฐํ๊ธฐ ๐ (0) | 2022.05.29 |