Call an action from within another action

2020-05-14 04:12发布

I have the following setup for my actions:

get1: ({commit}) => {
  //things
  this.get2(); //this is my question!
},
get2: ({commit}) => {
  //things
},

I want to be able to call one action from within another, so in this example I want to be able to call get2() from within get1(). Is this possible, and if so, how can I do it?

标签: vue.js vuex
2条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-05-14 05:09
export actions = {
  GET_DATA (context) {
     // do stuff
     context.dispatch('GET_MORE_DATA');
  },

  GET_MORE_DATA (context) {
    // do more stuff
  }
}
查看更多
我只想做你的唯一
3楼-- · 2020-05-14 05:13

You have access to the dispatch method in the object passed in the first parameter:

get1: ({ commit, dispatch }) => {
  dispatch('get2');
},

This is covered in the documentation.

查看更多
登录 后发表回答