๋ทฐ์์ค๋ฅผ ์์๋ณด๊ธฐ ์ํด ๋ฒํผ์ผ๋ก ์ซ์๋ฅผ ๋๋ฆฌ๊ณ ์ค์ผ ์ ์๋ ์นด์ดํฐ ์ฑ์ ํตํด
Vue CLI๋ก ํ๋ก์ ํธ๋ฅผ ์์ฑํ ๋ค์ ์๋์ ๊ฐ์ด Parent, Child ์ปดํฌ๋ํธ๋ฅผ ์ ์ํ๋ค.
- vue CLI๋ก ์ํ๋ ํด๋์ ํ๋ก์ ํธ๋ฅผ ์์ฑํ๊ณ , ์์ ๊ฐ์ด ํด๋ ๊ตฌ์กฐ๋ฅผ ๋ง๋ ๋ค.
App.vue
: ์์ ์ปดํฌ๋ํธ(Parent)child.vue
: ํ์ ์ปดํฌ๋ํธ(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>
...
- ๋ฒํผ์ ํด๋ฆญํ๋ฉด parent์ child์ปดํฌ๋ํธ๊ฐ ํจ๊ป ๋ณ๋๋๋ค.
- ์์ ์ปดํฌ๋ํธ์์ ํ์ ์ปดํฌ๋ํธ์๊ฒ props(counter)๋ฅผ ์ ๋ฌํ๋ ๊ธฐ๋ณธ์ ์ธ ์ปดํฌ๋ํธ ํต์ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ด๋ค.
- ํ์ง๋ง ์ปดํฌ๋ํธ์ ๊ฐฏ์๊ฐ ๋ฌดํ์ ๋ง์ ์ง๋ค๋ฉด, ์ต์์ ์ปดํฌ๋ํธ์์ ๋งจ ์๋์ ์ปดํฌ๋ํธ์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๊ธฐ ์ํด ์ค๊ฐ ๊ณ์ธต ์ปดํฌ๋ํธ์๊ฒ props, event๋ฅผ ์ ์ธํด์ค์ผํ๋ค.
- ํจ์จ์ ์ธ ๋ฐ์ดํฐ ๊ด๋ฆฌ๋ฅผ ์ํด vuex ๋ฅผ ์ด์ฉํด๋ณด์
-
ํด๋น ํ๋ก์ ํธ ํด๋ cmd์ฐฝ์ ์ด์ด์
npm install vuex
๋ฅผ ์ ๋ ฅํด Vuex๋ฅผ ์ค์นํ๋ค. -
Vuex ์ค์น ํ์ธ์
package.json
ํ์ผ "dependencies" ์์ ํ ์ ์๋ค. -
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) });
-
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 } });
- 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 ์ ๋ฌํ์ง ์์
Parent ์ปดํฌ๋ํธ์์ ๊ด๋ฆฌํ๋ counter ๋ฐ์ดํฐ๋ฅผ vuex์ state์ ๋๊ฒจ์ฃผ์๋ค.
Child ์ปดํฌ๋ํธ์์ ์ ๊ทผํ๋ Parent ์ปดํฌ๋ํธ์ data ์์ฑ์ด vuex๋ก ๊ฐ๊ธฐ ๋๋ฌธ์ ์ด์ Child์์๋ vuex์ state๋ฅผ ๋ฐ๋ผ๋ณด๋ฉด ๋๋ค.
์ด์ Parent์ Child ๋ชจ๋ state๋ฅผ ์ ๊ทผํ ์ ์๊ฒ ๋์๊ณ , ์ด๋ค ์ปดํฌ๋ํธ๋ ์ด์ vuex๋ก counter๋ฅผ ์ ๊ทผํ ์ ์๊ฒ ๋์๋ค.
- child ์ปดํฌ๋ํธ์๋ ๋ทฐ์์ค๋ฅผ ์ ์ฉ์์ผ์ค๋ค.
<template>
<div>
<hr>
Child counter : {{ $store.state.counter}}<br>
<button>+</button>
<button>-</button>
</div>
</template>
<script>
export default{
// props : ['num']
}
</script>