axios get请求获取数据后怎么拿出来?

是这样的,我在另外一个文件写了一个api接口,想在组件中使用那个api函数。问题出现在axios写的函数里面,我想拿到里面的数据,如果单纯的console.log是可以输出的,但我想把axios.get这个请求返回这些数据。下面是api函数的代码。

https://img1.mukewang.com/5c7a1c4100019b5406580406.jpg

然后想在组件中输出

https://img3.mukewang.com/5c7a1c440001f54306120287.jpg


输出的值是undefined的,在api函数那边写成return res.data也是undefined.请问有什么解决的办法吗?

MMTTMM
浏览 6835回答 2
2回答

jeck猫

apiAxios内axios then回调不能直接return,只能用callback方式function apiAxios(city, callback) {    ...    .then(res => {      callback(res)    })}get: function(city, callback) {  apiAxios(city, callback);}this.$api.get('abc', function(res) {  console.log(res.data);})

蓝山帝景

apiAxios 函数做点改造function apiAxios(){&nbsp; &nbsp; url += city;&nbsp; &nbsp; return axios.get(url, {header:"..."})}export default {&nbsp; &nbsp; get: apiAxios}// 输出.vue<script>&nbsp; &nbsp; export default {&nbsp; &nbsp; &nbsp; &nbsp; created(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.$api.get("...").then(res => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(res)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }</script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript