Vue.js vuex - swkim0128/PARA GitHub Wiki
Vue.js application์ ๋ํ ์ํ๊ด๋ฆฌํจํด + ๋ผ์ด๋ธ๋ฌ๋ฆฌ
application ๋ชจ๋ component๋ค์ ์ค์ ์ ์ฅ์ ์ญํ (๋ฐ์ดํฐ ๊ด๋ฆฌ)
์์(๋ถ๋ชจ) ํ์(์์)์ ๋จ๊ณ๊ฐ ๋ง์ด ๋ณต์กํด ์ง๋ค๋ฉด ๋ฐ์ดํฐ์ ์ ๋ฌํ๋ ๋ถ๋ถ์ด ๋งค์ฐ ๋ณต์กํด ์ง
application์ด ์ฌ๋ฌ ๊ตฌ์ฑ ์์๋ก ๊ตฌ์ฑ๋๊ณ ๋ ์ปค์ง๋ ๊ฒฝ์ฐ ๋ฐ์ดํฐ๋ฅผ ๊ณต์ ํ๋ ๋ฌธ์ ๊ฐ ๋ฐ์
-
CDN
</script>
-
npm
npm install vuex โsave
- State : ๋จ์ผ ์ํ ํธ๋ฆฌ๋ฅผ ์ฌ์ฉ. application๋ง๋ค ํ๋์ ์ ์ฅ์๋ฅผ ๊ด๋ฆฌ(data)
- Getters : Vue Instacne ์ Computed์ ๊ฐ์ ์ญํ . State๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๊ณ์ฐ(computed)
- Mutations : State์ ์ํ๋ฅผ ๋ณ๊ฒฝํ๋ ์ ์ผํ ๋ฐฉ๋ฒ (๋๊ธฐ methods)
- Actions : ์ํ๋ฅผ ๋ณ์ด ์ํค๋ ๋์ ์ก์ ์ผ๋ก ๋ณ์ด์ ๋ํ ์ปค๋ฐ ์ฒ๋ฆฌ (๋น๋๊ธฐ methods)
-
medule ์์คํ ๊ณผ ํจ๊ป ์ฌ์ฉ์ Vue.use()๋ฅผ ํตํด Vuex ์ค์
์ ์ฅ์์์ data ์์ฑ์ ์ญํ
application์์ ๊ณต์ ํด์ผ ํ data๋ฅผ ๊ด๋ฆฌ
State์ ์ ๊ทผํ๋ ๋ฐฉ์ : this.$store.state.data_name
-
component๊ฐ vuex์ state๋ฅผ ์ง์ ์ ๊ทผํ๋ ์ฝ๋๊ฐ ์ค๋ณต๋๋ค๋ฉด?
-
ํด๊ฒฐ : Store์ state๋ฅผ ์ฐธ์กฐํ๋ Getters๋ฅผ ํ์ฉ
-
์ ์
-
์ฌ์ฉ
this.$store.getters.countMsg;
-
getters๋ฅผ ์กฐ๊ธ ๋ ๊ฐ๋จํ๊ฒ ํธ์ถ
-
์ฃผ์ : Babel ๊ด๋ จ ์๋ฌ ๋ฐ์์ ํด๊ฒฐ
...mapGetters ๊ด๋ จ
es6 spread operation ๊ด๋ จ ์๋ฌ ๋ฐ์
- State์ ๊ฐ์ ๋ณ๊ฒฝํ๊ธฐ ์ํด์ ์ฌ์ฉ
- ๊ฐ ์ปดํฌ๋ํธ์์ State์ ๊ฐ์ ์ง์ ๋ณ๊ฒฝํ๋ ๊ฒ์ ๊ถ์ฅํ์ง ์๋ ๋ฐฉ์
- State์ ๊ฐ์ ์ถ์ ์ ์ํด ๋๊ธฐ์ ๊ธฐ๋ฅ์ ์ฌ์ฉ
- Mutations๋ ์ง์ ํธ์ถ์ด ๋ถ๊ฐ๋ฅํ๊ณ store.commit('์ ์๋ ์ด๋ฆ')์ผ๋ก ํธ์ถ
- ๋น๋๊ธฐ ์์ ์ ๊ฒฐ๊ณผ๋ฅผ ์ ์ฉํ๋ ค๊ณ ํ ๋ ์ฌ์ฉ
- Mutations๋ ์ํ ๊ด๋ฆฌ๋ฅผ ์ํด ๋๊ธฐ์ ์ผ๋ก ์ฒ๋ฆฌํ๊ณ ๋น๋๊ธฐ์ ์ธ ์ฒ๋ฆฌ๋ Actions๊ฐ ๋ด๋น
- Actions๋ ๋น๋๊ธฐ ๋ก์ง์ ์ฒ๋ฆฌ๊ฐ ์ข ๋ฃ๋๋ฉด Mutations๋ฅผ ํธ์ถ
-
step00 : Vuex ์ ์ฉ์ ์ฝ๋
-
step01 : vuex์ฌ์ฉ
-
step02
-
step04
-
step05
-
step06
-
step06
-
step07