Vue.js
[Vue] vuex actions 에서 다른 action 호출하기
bryan.oh
2021. 1. 13. 18:29
반응형
actions 에서 다른 action 호출하기
아래와 같은 소스가 있을 때 method_A 에서 method_B를 호출하는 방법은,
const state = {
something: ''
}
const mutations = {
setSomething (state, message) {
state.something = message
}
}
const actions = {
method_A({ commit }) {
// method_B 호출
commit('setSomething', 'from method A')
},
method_B({ commit }) {
// .. do something
}
}
1. method_A 에 파라메터에 dispatch 추가
2. dispatch 를 이용하여 method_B 호출
method_A({ commit, dispatch }) {
dispatch('method_B') // 메소드명을 문자열로 호출합니다.
commit('setSomething', 'from method A')
}
method_B 에 파라메터를 받는 다면
const actions = {
method_A({ commit, dispatch }) {
dispatch('method_B', 'something') // 두번째 부터 파라메터 사용
commit('setSomething', 'from method A')
},
method_B({ commit }, msg) { // comm
// do something with param:msg
}
}
728x90
반응형