Today I Learn 220512
์ค๋์ ์ด์ ํ๋ ์ค์ต์ ๋ค์ ์งํํ๋๋ฐ ์ฝ๊ฐ์ ๋ฏธ์ ์์๋ฅผ ์์ฃผ ์กฐ๊ธ ๊ฐ๋ฏธํด์ฃผ์๋ค. ์ด์ ๋ ์ฝ๋๋ฅผ ๋ฐ๋ผ์น๋๋ผ ํ๋ฆ์ ์ด์ง ๋์น๋ฉด์ ํ๋ ๊ฒฝํฅ์ด ์์๋๋ฐ, ์ค๋์ console.log๋ฅผ ํตํด ํ๋์ฉ ํ๋ฆ์ ๋ฐ๋ผ๊ฐ๋ค ๋ณด๋ ๋ ์ดํด๊ฐ ์ ๋๊ณ vuex์ ๋ํด ๋ ์ ์ ์ ์์๋ค.
vuex๋ core concepts์ธ actions, getters, mutations, state๋ง ์ ์์๋๋ฉด ์ํฉ์ ๋ฐ๋ผ ์ ์ ํ๊ฒ ์ธ ์ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ฐ๋ค.
์ค๋ ๋ค์ ๊ตฌํํ ์์ค์ฝ๋
index.js
import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)
export default new Vuex.Store({
plugins: [
createPersistedState()
],
state: {
todos : [
{
title: "vuex๊ณต๋ถ",
done: false,
date: new Date().getTime(),
},{
title:"์ ์ฌ ๋จน๊ธฐ",
done: false,
date: new Date().getTime()+1,
},{
title:"๋ฌผ 2๋ฆฌํฐ ๋ง์๊ธฐ",
done: false,
date: new Date().getTime()+2,
}
]
},
getters: {
done(state){
return state.todos.filter(todo=> todo.done===true).length
},
undone(state){
return state.todos.filter(todo=> todo.done===false).length
},
all(state){
return state.todos.length
}
},
mutations: {
CREATE_TODO(state,todoItem){
// console.log(`์๋
${todoItem}`)
// console.dir(todoItem)
state.todos.push(todoItem)
},
DELETE_TODO(state,todoItem){
const todoIndex = state.todos.indexOf(todoItem)
state.todos.splice(todoIndex,1)
},
UPDATE_TODO(state,todoItem){
//state๋ฅผ ์๋ฐ๊ฟ์ฃผ๋ ์ฝ๋์.. mutations๋ state๋ฅผ ๋ฐ๊ฟ์ผํจ!
// const index = state.todos.indexOf(todo)
// state.todos[index].done = !state.todos[index].done
// todo.done = !todo.done
state.todos = state.todos.map(todo=>{
if(todo===todoItem){
//์์ ๋ก์ง
todo.done = !todo.done
}
return todo
})
}
},
actions: {
createTodo({commit},todoItem){
// console.log(`์๋
${todoItem}`)
// console.dir(todoItem)
commit('CREATE_TODO',todoItem)
},
deleteTodo({commit},todoItem){
commit('DELETE_TODO',todoItem)
},
updateTodo({commit},todoItem){
commit('UPDATE_TODO',todoItem)
}
},
modules: {
}
})
App.vue
<template>
<div id="app">
<h1>Iseo's Todo!</h1>
All : {{all}}
<br>
Done : {{done}}
<br>
UnDone: {{ undone }}
<TodoList/>
<TodoForm/>
</div>
</template>
<script>
import TodoForm from '@/components/TodoForm.vue'
import TodoList from '@/components/TodoList.vue'
export default {
name: 'App',
components: {
TodoForm,
TodoList,
},
computed:{
done(){
return this.$store.getters.done
},
undone(){
return this.$store.getters.undone
},
all(){
return this.$store.getters.all
}
}
}
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
border: 3px solid black;
margin: 30px;
}
</style>
TodoList.vue
<template>
<div class="TodoList">
<h2>์ค๋ ํด์ผํ ์ผ!</h2>
<TodoListItem v-for="todo in todos" :key="todo.date" :todo="todo"/>
</div>
</template>
<script>
import TodoListItem from '@/components/TodoListItem.vue'
import {mapState} from 'vuex'
export default {
name: 'TodoList',
components: {
TodoListItem,
},
computed:{
...mapState(['todos'])
// todos(){
// return this.$store.state.todos
// }
}
}
</script>
<style>
.TodoList{
border : 3px solid tan;
margin-bottom: 20px;
padding: 20px;
margin: 20px;
}
</style>
TodoListItem.vue
<template>
<div class="TodoListItem">
<!-- <p>TodoListItem</p> -->
<span :class="{'is-done':todo.done}" @click="updateTodo">{{todo.title}}</span>
<button @click="deleteTodo">x</button>
</div>
</template>
<script>
export default {
name: 'TodoListItem',
props:{
todo:{
type: Object,
}
},
methods: {
deleteTodo(){
this.$store.dispatch('deleteTodo',this.todo)
},
updateTodo(){
this.$store.dispatch('updateTodo',this.todo)
},
},
created(){
console.log(this.$store.getters)
}
}
</script>
<style>
.TodoListItem{
border : 3px solid gold;
margin: 10px;
}
.is-done{
text-decoration-line: line-through;
}
</style>
TodoForm.vue
<template>
<div @click="FormClick" class="TodoForm">
<h4>๋ญ ํด์ผ ํ์ง?</h4>
<input type="text" ref="cursor" class="test" v-model="todoTitle" @keyup.enter="createTodo">
<button @click="createTodo">+</button>
<h6>แฆ(ò_óห)แค</h6>
</div>
</template>
<script>
export default {
name: 'TodoForm',
data(){
return {
todoTitle:'',
}
},
// created(){
// console.log(this.ssafy)
// console.log(this.todoTitle)
// },
// mounted(){
// console.log(this.ssafy)
// console.log(this.todoTitle)
// },
// updated(){
// console.log(this.ssafy)
// console.log(this.todoTitle)
// },
methods :{
createTodo(){
const todoItem = {
title:this.todoTitle,
done: false,
date: new Date().getTime()+2,
}
if(this.todoTitle){
this.$store.dispatch('createTodo', todoItem)
}
this.todoTitle=''
},
FormClick(){
this.$refs.cursor.focus()
}
}
}
</script>
<style>
.TodoForm{
border : 3px solid darkblue;
margin: 20px;
}
</style>
์ด์ ์ ๋ค๋ฅด๊ฒ TodoForm์ ํด๋ฆญ ์ cursor๊ฐ ์ผ์ง๊ฒ ํ๋ ๊ฒ์ develop ํ๋ค.
div์ click์ด๋ฒคํธ์ FormClick๋ฉ์๋๋ฅผ ์ฐ๊ฒฐ์์ผ์ฃผ๊ณ ,
input์ฐฝ์ ref๊ฐ์ ๋ฃ์ด FormClick ๋ฉ์๋์ $refs๋ก input์ ref๋ฅผ ์ฐ๊ฒฐํด focusํด์ฃผ๋ฉด ๋๋ค!
*์ฐธ๊ณ ๋ธ๋ก๊ทธ
'๐๐จ๐๐๐ฒ ๐ ๐๐๐๐ซ๐ง' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐๐จ๐๐๐ฒ ๐ ๐๐๐๐ซ๐ง 2022.06.15.์ (0) | 2022.06.15 |
---|---|
๐๐จ๐๐๐ฒ ๐ ๐๐๐๐ซ๐ง 2022.06.14.ํ (0) | 2022.06.14 |
๐๐จ๐๐๐ฒ ๐ ๐๐๐๐ซ๐ง 2022.05.11.์ (0) | 2022.05.11 |
๐๐จ๐๐๐ฒ ๐ ๐๐๐๐ซ๐ง 2022.05.10.ํ (0) | 2022.05.10 |
๐๐จ๐๐๐ฒ ๐ ๐๐๐๐ซ๐ง 2022.05.09.์ (0) | 2022.05.09 |
๋๊ธ