是这样的,我在另外一个文件写了一个api接口,想在组件中使用那个api函数。问题出现在axios写的函数里面,我想拿到里面的数据,如果单纯的console.log是可以输出的,但我想把axios.get这个请求返回这些数据。下面是api函数的代码。然后想在组件中输出输出的值是undefined的,在api函数那边写成return res.data也是undefined.请问有什么解决的办法吗?
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(){ url += city; return axios.get(url, {header:"..."}) } export default { get: apiAxios } // 输出.vue <script> export default { created(){ this.$api.get("...").then(res => { console.log(res) }) } } </script>