在商店中调度多个动作

时间:2020-03-10 11:08:00

标签: vue.js vuejs2 vuex vuex-modules

假设我在商店中有以下操作:

actions: {
  async actionA ({ commit }) {
    commit('gotData', await getData())
  },
  async actionB ({ dispatch, commit }) {
    await dispatch('actionA') // wait for `actionA` to finish
    commit('gotOtherData', await getOtherData())
  },
async actionC ({ commit }) {
    commit('gotExtraData', await getData())
  },
... and many more.

注意:所有都是独立的动作。 现在,我想在代码的不同级别上分派多个操作。 这是我目前的工作方式。

await Promise.all([this.$store.dispatch(`${storeUrl}/actionA`,
this.$store.dispatch(`${storeUrl}/actionC`), ...and many more)]`;

我可以使用一个调度语句来调用操作,而不是那么多单独的调度调用? 像这样的东西:

await this.$store.dispatch(storeUrl, [...all individual actions here]); 

0 个答案:

没有答案