我不确定为什么无法从我的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
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')
使用您的代码中的
- shop
this.$store.state.shop
或如果要使用多个状态道具,则使用mapState。还要确保不要从突变中返回任何数据。他们必须更改状态,而不返回其道具。