λ€μ΄κ°λ©°
μ΄ κΈμ Vuex μμνκΈ° 1, Vuex μμνκΈ° 2μ μ΄μ΄ Vuex μ λ§μ§λ§ λΆλΆμΈ Actions μ ν΄λ ꡬ쑰ν λ°©λ²μ μκ°ν©λλ€. μ§λ κΈμμλ mutations κΉμ§ λ€λ€μ΅λλ€.
Actions λ?
Mutations μλ μμ°¨μ μΈ λ‘μ§λ€λ§ μ μΈνκ³ Actions μλ λΉ μμ°¨μ λλ λΉλκΈ° μ²λ¦¬ λ‘μ§λ€μ μ μΈνλ€. κ·Έλ λ€λ©΄ μ μ²λ¦¬ λ‘μ§μ μ±κ²©μ λ°λΌ Mutations κ³Ό Actions λ‘ λλ λ±λ‘ν΄μΌ ν κΉ?
Mutations μ λν΄ μ κΉ μ§μ΄λ³΄λ©΄, Mutations μ μν μμ²΄κ° State κ΄λ¦¬μ μ£Όμμ μ λκ³ μλ€. μνκ΄λ¦¬ μμ²΄κ° ν λ°μ΄ν°μ λν΄ μ¬λ¬ κ°μ μ»΄ν¬λνΈκ° κ΄μ¬νλ κ²μ ν¨μ¨μ μΌλ‘ κ΄λ¦¬νκΈ° μν¨μΈλ° Mutations μ λΉλκΈ° μ²λ¦¬ λ‘μ§λ€μ΄ ν¬ν¨λλ©΄ κ°μ κ°μ λν΄ μ¬λ¬ κ°μ μ»΄ν¬λνΈμμ λ³κ²½μ μμ²νμ λ, κ·Έ λ³κ²½ μμ νμ μ΄ μ΄λ ΅κΈ° λλ¬Έμ΄λ€.
μ΄λ¬ν λ¬Έμ λ₯Ό λ°©μ§νκΈ° μν΄ λΉλκΈ° μ²λ¦¬ λ‘μ§μ Actions μ λκΈ° μ²λ¦¬ λ‘μ§μ Mutations μ λλ ꡬννλ€.
λ°λΌμ, setTimeout()
μ΄λ μλ²μμ http ν΅μ μ²λ¦¬ κ°μ΄ κ²°κ³Όλ₯Ό λ°μμ¬ νμ΄λ°μ΄ μμΈ‘λμ§ μμ λ‘μ§μ Actions μ μ μΈνλ€.
Actions λ±λ‘
Vuex μ Actions λ₯Ό λ±λ‘νλ λ°©λ²μ λ€λ₯Έ μμ±κ³Ό μ μ¬νλ€. actions λ₯Ό μ μΈνκ³ action method λ₯Ό μΆκ°ν΄μ€λ€.
// store.js
export const store = new Vuex.Store({
// ...
mutations: {
addCounter: function (state, payload) {
return state.counter++;
}
},
actions: {
addCounter: function (context) {
// commit μ λμμΈ addCounter λ mutations μ λ©μλλ₯Ό μλ―Ένλ€.
return context.commit('addCounter');
}
}
});
μνκ° λ³ννλ κ±Έ μΆμ νκΈ° μν΄ actions λ κ²°κ΅ mutations μ λ©μλλ₯Ό νΈμΆ(commit) νλ κ΅¬μ‘°κ° λλ€.
// store.js
export const store = new Vuex.Store({
actions: {
getServerData: function (context) {
return axios.get("sample.json").then(function() {
// ...
});
},
delayFewMinutes: function (context) {
return setTimeout(function () {
commit('addCounter');
}, 1000);
}
}
});
μμ²λΌ HTTP get μμ²μ΄λ setTimeout κ³Ό κ°μ λΉλκΈ° μ²λ¦¬ λ‘μ§λ€μ actions μ μ μΈν΄μ€λ€.
Actions μ¬μ©
μμμλ mutations λ₯Ό μ΄μ©νμ¬ counter λ₯Ό νλμ© λλ Έλ€. μ΄λ²μ actions λ₯Ό μ΄μ©ν΄λ³΄μ. actions λ₯Ό νΈμΆν λλ μλμ κ°μ΄ dispatch() λ₯Ό μ΄μ©νλ€.
// App.vue
methods: {
// Mutations λ₯Ό μ΄μ©ν λ
addCounter() {
this.$store.commit('addCounter');
}
// Actions λ₯Ό μ΄μ©ν λ
addCounter() {
this.$store.dispatch('addCounter');
}
},
μ 체 ꡬ쑰λμμ dispatch μ λμμ 보면
Actions μ μΈμ κ° λκΈ°κΈ°
Actions μ μΈμλ₯Ό λκΈ°λ λ°©λ²μ Mutations μ μ μ¬νλ€.
<!-- by μ duration λ±μ μ¬λ¬ μΈμ κ°μ λκΈΈ κ²½μ°, κ°μ²΄μμ key - value ννλ‘ μ¬λ¬ κ°μ λκΈΈ μ μλ€ -->
<button @click="asyncIncrement({ by: 50, duration: 500 })">Increment</button>
export const store = new Vuex.Store({
actions: {
// payload λ μΌλ°μ μΌλ‘ μ¬μ©νλ μΈμ λͺ
asyncIncrement: function (context, payload) {
return setTimeout(function () {
context.commit('increment', payload.by);
}, payload.duration);
}
}
})
mapActions
mapGetters, mapMutations ν¬νΌ ν¨μλ€κ³Ό λ§μ°¬κ°μ§λ‘ mapActions λ λμΌν λ°©μμΌλ‘ μ¬μ©ν μ μλ€.
import {mapActions} from 'vuex';
export default {
methods: {
...mapActions([
'asyncIncrement',
'asyncDecrement'
])
},
}
ν΄λ ꡬ쑰ν & Namespacing
μ€κ° ν¬κΈ° μ΄μμ 볡μ‘ν μ±μ μ μν λ getters & mutations & actions
μ μ΄λ¦μ μ μΌνκ² μ νμ§ μμΌλ©΄ namespace μΆ©λμ΄ λλ€.
λ°λΌμ, λ€μμ€νμ΄μ€λ₯Ό ꡬλΆνκΈ° μν΄ types.js
λ‘ κ° μμ±μ μ΄λ¦λ€μ λΉΌκ³ store.js
μ κ° μ»΄ν¬λνΈμ import νμ¬ μ¬μ©νλ λ°©λ²μ΄ μλ€.
νΉμ modules λΌλ ν΄λλ‘ λ§λ€μ΄ κ° λ¨μλ³λ‘ νμΌμ μͺΌκ°μ κ΄λ¦¬νλ λ°©λ²λ μλ€.
μκ°λ³΄λ€ 볡μ‘νλ―λ‘ μ±μ΄ 컀μ μ€ν μ΄μμ μ±μμλ§ μ¬μ©νλκ² μ’μ λ―νλ€. κ°λ¨ν νλ©΄ κ°λ°μλ μ€νλ € λ°°λ³΄λ€ λ°°κΌ½μ΄ ν΄ μ μλ€.
λ§λ¬΄λ¦¬
μ§λ 2κ°μ κΈκ³Ό ν¨κ» μ΄ 3νΈμ Vuex κ΄λ ¨ κΈμ ν΅ν΄, Vue λ‘ μ±μ κ°λ°ν λ λ ν¨μ¨μ μΌλ‘ μ½λμ λ°μ΄ν°λ₯Ό κ΄λ¦¬ν μ μλ μνκ΄λ¦¬μ λν΄ μμ보μμ΅λλ€. μ€ν μ΄μμ μ±μλ νμμ μΌλ‘ μ¨μΌνλ 보쑰 λΌμ΄λΈλ¬λ¦¬λΌκ³ λ³΄μ€ μ μλλ°μ. Vuex κ° κ°μ Έλ€ μ£Όλ μ΄μ λ ν¬μ§λ§, μ λ§ κ°λ¨ν νλ©΄μ λ§λ€ λλ μ€νλ € μ΄κΈ° μΈν νλλ° μκ°μ΄ λ§μ΄ 걸릴 μ μμΌλ μ μ νμκΈ° λ°λλλ€ :)
Vuex μμνκΈ° 1 - μνκ΄λ¦¬ μκ° & States
Vuex μμνκΈ° 2 - Getters & Mutations
κΈλ³΄λ€ λ μ½κ² λ°°μ°λ μ¨λΌμΈ κ°μ
μ’ λ μΉμ νκ³ μμΈν μ€λͺ μ μνμ λ€λ©΄ μλ κ°μ’λ₯Ό μ΄μ©ν΄λ³΄μλ κ²λ μ’μ κ² κ°μμ π








