简体   繁体   English

多个 url 获取异步等待 axios VUE.JS

[英]multiple url get async await axios VUE.JS

Probably this is a very stupid question, i'm new in vue.js and javascript, so please forgive me if the question is not properly explained or the answer is simple... I have a problem I wanted to get from two different api and then display it for.可能这是一个非常愚蠢的问题,我是 vue.js 和 javascript 的新手,所以如果问题没有得到正确解释或答案很简单,请原谅我......我有一个问题我想从两个不同的 api 得到然后显示它。

async mounted() {
const { data } = await this.axios.get(
  "http://some-ip/api/get"
).then(response => {
    this.items = response.data.data.items
  })
;
const { data2 } = await this.axios.get(
  "http://some-ip/api2/get"
).then(response => {
    this.items2 = response.data.data.items
  })
;

First api works perfectly, the second no works... Any good soul who can help this newbie?第一个 api 工作完美,第二个没有工作......任何可以帮助这个新手的好人? (if can explain the why of the solution as well, for understand, will be amazing!!) (如果也能解释解决方案的原因,理解,将是惊人的!!)

Thanks in advance!!提前致谢!!

It seems like you're trying to destructure data2 from Axios response which doesn't exist on that object, only data does.似乎您正试图从该对象上不存在的 Axios 响应中解构data2 ,只有data存在。 You have to use data to get the actual response data.您必须使用data来获取实际的响应数据。

Axios response schema for reference: https://axios-http.com/docs/res_schema axios 响应模式供参考: https ://axios-http.com/docs/res_schema

Consider this example as it destructures the data props but sets the name to data2:考虑这个例子,因为它解构了数据道具,但将名称设置为 data2:

const { data: data2 } = await this.axios.get(
  "http://some-ip/api2/get"
).then(response => {
    this.items2 = response.data.data.items
  })
;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM