Vuex Store中的Axios返回承诺而不是数据

问题描述 投票:0回答:1

我不确定为什么无法从我的Vuex存储中的axios.get获取所需的数据。

我已经将action设置为commit,对我的mutation的更改如下:

      mutations: {
        updateShop: (state, payload ) => {
          state.shop = payload;
          return state.shop;
        }
      },
      actions: {
        getShop: ({ commit }) => {
          return axios.get("/admin/wine_app/shops").then(response => {
            debugger; // I DO have data here??
            commit('updateShop', response.data);
          });
        }
      }

但是当我用debugger停止它时,我确实有数据,但是当我在组件中使用getShop操作时,我看到了承诺被返回。

知道为什么吗?

编辑:它可能还没有准备好!我在控制台中看到此]

    Promise {<pending>}
    __proto__: Promise
    [[PromiseStatus]]: "pending"
    [[PromiseValue]]: undefined
axios vuex
1个回答
0
投票
    执行操作
  1. getShop异步
getShop: async ({ commit }) => { const response = await axios.get("/admin/wine_app/shops"); debugger; // I DO have data here?? commit('updateShop', response.data); }
    等待您调用的动作
  • await this.$store.dispatch('getShop')
      使用您的代码中的
    1. shop
  • 状态道具this.$store.state.shop
    或如果要使用多个状态道具,则使用mapState。

    还要确保不要从突变中返回任何数据。他们必须更改状态,而不返回其道具。

    © www.soinside.com 2019 - 2024. All rights reserved.