ahn-sujin / vuex

๐Ÿ“— vuex ๊ธฐ์ดˆ ์ด๋ก  ์ •๋ฆฌ ๋ฐ ์‹ค์Šต

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

๐Ÿ’ก vuex ์‹œ์ž‘ํ•˜๊ธฐ

1. Vuex์™€ State

๋ทฐ์—‘์Šค๋ฅผ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด ๋ฒ„ํŠผ์œผ๋กœ ์ˆซ์ž๋ฅผ ๋Š˜๋ฆฌ๊ณ  ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ์นด์šดํ„ฐ ์•ฑ์„ ํ†ตํ•ด 
Vue CLI๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•œ ๋‹ค์Œ ์•„๋ž˜์™€ ๊ฐ™์ด Parent, Child ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ œ์ž‘ํ•œ๋‹ค. 

1-1. ๊ฐ„๋‹จํ•œ Vue App๊ตฌ์„ฑ

image

  • vue CLI๋กœ ์›ํ•˜๋Š” ํด๋”์— ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์œ„์™€ ๊ฐ™์ด ํด๋” ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“ ๋‹ค.
    • App.vue : ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ(Parent)
    • child.vue : ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ(Child)

1-2. parent, child ์ปดํฌ๋„ŒํŠธ ์„ค์ •

App.vue (parent)

<template>
  <div id="app">
    Parent counter : {{ counter }} <br>
    <button v-on:click = "addCounter">+</button>
    <button v-on:click = "subCounter">-</button>
    <!--  child ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋“ฑ๋กํ•˜๊ณ  counter ๋ฐ์ดํ„ฐ ์†์„ฑ์„ props๋กœ ์ „๋‹ฌํ•œ๋‹ค. -->
    <Child v-bind:num = "counter"></Child>
  </div>
</template>

<script>
  // child ์ปดํฌ๋„ŒํŠธ ์—ฐ๊ฒฐ
  import Child from './components/Child.vue'

  export default{
    data(){
      return{
        counter: 0
      };
    },
    methods:{
      addCounter(){
        this.counter++;
      },
      subCounter(){
        this.counter--;
      }
    }, 
    components: {
      // child ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋กœ ๋“ฑ๋ก
      'Child' : Child
    }
  }
</script>
...

Child.vue (Child)

<template>
  <div>
    <hr>
    Child counter : {{ num }} <br>
    <button>+</button>
    <button>-</button>
  </div>
</template>

<script>
  // ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋‚ด๋ ค์ค€ counter ์†์„ฑ์„ num๋กœ ๋ฐ›์Œ
  props: ["num"]
</script>
...

image

  • ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด parent์™€ child์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ•จ๊ป˜ ๋ณ€๋™๋œ๋‹ค.
  • ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ props(counter)๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ธฐ๋ณธ์ ์ธ ์ปดํฌ๋„ŒํŠธ ํ†ต์‹  ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
  • ํ•˜์ง€๋งŒ ์ปดํฌ๋„ŒํŠธ์˜ ๊ฐฏ์ˆ˜๊ฐ€ ๋ฌดํ•œ์ • ๋งŽ์•„ ์ง„๋‹ค๋ฉด, ์ตœ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋งจ ์•„๋ž˜์˜ ์ปดํฌ๋„ŒํŠธ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ์ค‘๊ฐ„ ๊ณ„์ธต ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ props, event๋ฅผ ์„ ์–ธํ•ด์ค˜์•ผํ•œ๋‹ค.
  • ํšจ์œจ์ ์ธ ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด vuex ๋ฅผ ์ด์šฉํ•ด๋ณด์ž

1-3. vuex ์„ค์น˜ ๋ฐ ๋“ฑ๋ก

  • ํ•ด๋‹น ํ”„๋กœ์ ํŠธ ํด๋” cmd์ฐฝ์„ ์—ด์–ด์„œ npm install vuex ๋ฅผ ์ž…๋ ฅํ•ด Vuex๋ฅผ ์„ค์น˜ํ•œ๋‹ค.

  • Vuex ์„ค์น˜ ํ™•์ธ์€ package.json ํŒŒ์ผ "dependencies" ์—์„œ ํ•  ์ˆ˜ ์žˆ๋‹ค.

    image

  • vuex๋ฅผ ๋“ฑ๋กํ•  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ํ•˜๋‚˜ ์ƒ์„ฑํ•œ๋‹ค.

    store.js

    import Vue from "vue";
    import Vuex from "vuex";
    
    Vue.use(Vuex);
    
    export const store = new Vuex.Store({
    
    });
  • main.js ์— store.js๋ฅผ ๋ถˆ๋Ÿฌ์™€ ๋“ฑ๋กํ•ด์ค€๋‹ค.

    main.js

    import Vue from "vue";
    import App from "./App.vue";
    // store.js๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์ฝ”๋“œ
    import { store } from "./store";
    
    new Vue({
      el: "#app",
      // ๋ทฐ ์ธ์Šคํ„ด์Šค์˜ store ์†์„ฑ์— ์—ฐ๊ฒฐ
      store: store,
      render: h => h(App)
    });

1-4. state ๋“ฑ๋ก

  • state์— ์ •์˜๋œ counter์†์„ฑ์€ App.js(parent ์ปดํฌ๋„ŒํŠธ)์—์„œ ์‚ฌ์šฉํ•˜๋˜ data์†์„ฑ counter์™€ ๋™์ผํ•œ ์—ญํ• ์ด๋‹ค.

    store.js

    import Vue from "vue";
    import Vuex from "vuex";
    
    Vue.use(Vuex);
    
    export const store = new Vuex.Store({
        //counter๋ผ๋Š” state ์†์„ฑ์„ ์ถ”๊ฐ€
        state: { //state๋Š” ์ปดํฌ๋„ŒํŠธ ๊ฐ„์— ๊ณต์œ ํ•  data์†์„ฑ
          counter: 0
        }
    });

1-5. state ์ ‘๊ทผ

  • state์— ๋“ฑ๋กํ•œ counter์†์„ฑ์€ <template>์ฝ”๋“œ์—์„œ $store.state.counter๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

App.vue

<template>
<div id="app">
  Parent counter : {{ $store.state.counter }} <br>
  <button v-on:click = "addCounter">+</button>
  <button v-on:click = "subCounter">-</button>
  <!-- <Child v-bind:num = "counter"></Child> -->
  <Child></Child>
</div>

</template>

<script>
import Child from './components/Child.vue'

export default{
  // data(){
  //   return{
  //     counter: 0
  //   };
  // },
  methods:{
    addCounter(){
      this.$store.state.counter++;
    },
    subCounter(){
      this.$store.state.counter--;
    }
  }, 
  components: {
    'Child' : Child
  }

}

</script>
  • ๋‹ฌ๋ผ์ง„ ์ 
    • data ์†์„ฑ์œผ๋กœ ์„ ์–ธํ•œ counter ๊ฐ’ ์ œ๊ฑฐ
    • child ์ปดํฌ๋„ŒํŠธ๋กœ counter ์ „๋‹ฌํ•˜์ง€ ์•Š์Œ

image

๐Ÿ“Œ๊ฒฐ๊ณผ

Parent ์ปดํฌ๋„ŒํŠธ์—์„œ ๊ด€๋ฆฌํ•˜๋˜ counter ๋ฐ์ดํ„ฐ๋ฅผ vuex์˜ state์— ๋„˜๊ฒจ์ฃผ์—ˆ๋‹ค.
Child ์ปดํฌ๋„ŒํŠธ์—์„œ ์ ‘๊ทผํ•˜๋˜ Parent ์ปดํฌ๋„ŒํŠธ์˜ data ์†์„ฑ์ด vuex๋กœ ๊ฐ”๊ธฐ ๋•Œ๋ฌธ์— ์ด์ œ Child์—์„œ๋Š” vuex์˜ state๋ฅผ ๋ฐ”๋ผ๋ณด๋ฉด ๋œ๋‹ค. 
์ด์ œ Parent์™€ Child ๋ชจ๋‘ state๋ฅผ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๊ณ , ์–ด๋–ค ์ปดํฌ๋„ŒํŠธ๋“  ์ด์ œ vuex๋กœ counter๋ฅผ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.

image

  • child ์ปดํฌ๋„ŒํŠธ์—๋„ ๋ทฐ์—‘์Šค๋ฅผ ์ ์šฉ์‹œ์ผœ์ค€๋‹ค.
<template>
<div>
    <hr>
    Child counter : {{ $store.state.counter}}<br>
    <button>+</button>
    <button>-</button>
</div>
</template>

<script>
export default{
    // props : ['num']
}
</script>

image

About

๐Ÿ“— vuex ๊ธฐ์ดˆ ์ด๋ก  ์ •๋ฆฌ ๋ฐ ์‹ค์Šต


Languages

Language:JavaScript 66.9%Language:Vue 26.7%Language:HTML 6.4%