【发布时间】:2021-08-11 15:00:09
【问题描述】:
我正在尝试使用 axios 执行多个请求并将响应保存在 vue 3 道具中。我知道不要改变道具,但这是另一个讨论。因为translation 是一个对象(变量通过引用传递),所以这里应该可以改变它。
正如我正确理解的那样,axios.all() 一次发出多个异步请求。这里的代码工作正常,axios请求成功,我可以在then()代码中console.log(response.data)他们。到目前为止一切顺利。
这行绝对行不通:this.translation[Object.keys(response.data)[index]] = response.data; - 道具translation 不会发生突变,我在控制台中也没有收到任何错误,但正如我写的文本“提交了所有axois 调用" 出现在控制台中。
如果我在 axios 调用之前 改变 prop,例如this.translation["foo"] = { bar: "foo" } 有效。
我在这里做错了什么?如何将此处的数据从 axios-response 传递给 prop 变量 translation?
export default {
props: {
marketplaces: Object,
translation: Object,
}
}
let requests = [axios1, axios2, ...]
axios
.all(requests)
.then(
axios.spread((...responses) => {
responses.forEach((response, index) => {
this.translation[Object.keys(response.data)[index]] = response.data;
});
console.log("submitted all axios calls");
})
)
.catch((error) => {
console.log("ERRRR:: ", error.response.data);
});
【问题讨论】:
-
FWIW,all 和 spread 都已过时。这些特性在 JS 中存在已久。
-
你有什么提示吗,2021年如何使用多个请求?
标签: javascript vue.js axios