๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
๐“๐จ๐๐š๐ฒ ๐ˆ ๐‹๐ž๐š๐ซ๐ง

๐“๐จ๐๐š๐ฒ ๐ˆ ๐‹๐ž๐š๐ซ๐ง 2022.05.12.๋ชฉ

by DevIseo 2022. 5. 12.

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ํ•ด์ฃผ๋ฉด ๋œ๋‹ค!

 

*์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ

https://bangj.tistory.com/107

๋Œ“๊ธ€