拿到axios的返回值

前端之家收集整理的这篇文章主要介绍了拿到axios的返回值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  • 想要拿到axios的返回值,需要return axios
  • 因为axios是异步,直接获取axios里面的返回值是拿不到值的,数据还没有返回来就已经执行了赋值,根本就不会拿到值。
 {    // 假设res是 [1,2,3]
            return res;  // 返回出来[1,3]
        })
        .catch(err => {
            console.log(err);
        })
}

let newdata = [];

getData.then(res => {
newdata = res // 可以把[1,3]赋给newdata
})

反例:

 {      // 假设res是 [1,3]
            return res;     
        })
        .catch(err => {
            console.log(err);
        })
}

let newdata = [];

newdata = getData(); // newdata -> 获取不到getData()的返回值

猜你在找的程序笔记相关文章