【问题标题】:Vuex: Sending arguments to actions from within vuexVuex:从 Vuex 内部向动作发送参数
【发布时间】:2020-09-03 23:39:12
【问题描述】:

以下是我的 vuex 操作之一。

updateProds: (context, data) => {
        axios.get(data.url)
        .then((response) => {
             context.dispatch( -----  );
            
        })
    },

在 Promise 返回内部,我想分派另一个动作,但希望传入来自该动作的响应。我该怎么做?

【问题讨论】:

  • “里面的Promise return”哪个promise返回?你能举个例子说明你想做什么吗?

标签: vue.js vuex


【解决方案1】:

如果您的操作返回一个值或一个可以解析为您想要的值的承诺,例如

actionOne: async ({ commit }, payload) => {
  // do something async, commit values, etc
  return actionResponse
}

那么你可以像这样继续链接承诺

axios.get(data.url)
  .then(({ data }) => context.dispatch("actionOne", data))
  .then(actionResponse => context.dispatch("actionTwo", actionResponse))

注意箭头函数的隐式返回值。 .then() 方法总是返回一个新的 Promise,它使用 return 值解析。

在更长的形式中,这看起来像

axios.get(data.url)
  .then(({ data }) => {
    return context.dispatch("actionOne", data))
  })
  .then(actionResponse => {
    return context.dispatch("actionTwo", actionResponse))
  })

【讨论】:

    猜你喜欢
    • 2019-06-11
    • 2018-05-07
    • 2019-07-18
    • 2021-09-30
    • 1970-01-01
    • 2021-04-23
    • 2021-08-05
    • 2022-01-17
    • 2019-02-02
    相关资源
    最近更新 更多