从VueX Action返回Axios Promise

时间:2019-03-08 09:12:32

标签: vue.js vuejs2 axios es6-promise vuex

我不知道,如果让vuex动作返回axios承诺(如下面的示例)是一种不好的方法,那么我不知道。之所以这样做,是因为我需要请求中vuex状态的一些值。谁能告诉我这是不好做还是完全可以? :)

/* Vuex action */
fetchSomething({ state }, id) {
  return getSomething(
    id,
    state.basket.shipping,
    state.basket.billing);
},

/* Method in vue component */
someMethod() {
  this.$store.dispatch('fetchSomething')
    .then(res => console.log(res));
}

1 个答案:

答案 0 :(得分:1)

我看不出这样做的意义。 Vuex操作的重点是执行异步工作,即检索数据,然后mutate Vuex存储。

商店被更改后,您可以access it from your Vue component。由于Vuex是Vue生态系统的一部分,因此遵循与Vue相同的反应性原则。如果您需要在检索到数据后执行某些操作,则可以在要检索的变量上设置观察者,或更常见的情况是将其用作计算属性。

相关问题