欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > 如何在函数中使用return返回axios的请求结果

如何在函数中使用return返回axios的请求结果

2024/10/25 23:11:14 来源:https://blog.csdn.net/qq_37703224/article/details/140354685  浏览:    关键词:如何在函数中使用return返回axios的请求结果

使用场景:在添加学生上课记录的时候,需要先获取学生的剩余课时,需要通过接口获取。所以需要封装一个方法,能够通过接口获取学生的课时数量。

解决方案:通过异步解决

封装方法的代码如下:

const getStudentCourseCount = async () => {let num = 0await axios({method: "get",url: `${server.apiBaseUrl}/zdppy_amcountdown_num?pk_id=${currentStudentId.value}`,}).then(resp => {const countNum = resp.data.data.results[0]num = countNum.num})return num
}

调用的方法也必须是异步的,一定要注意,方法如下:

const onFinish = async (values) => {console.log('Success:', values, formState);console.log("currentStudentId", currentStudentId.value);console.log("name", getStudentName(currentStudentId.value));const courseCount = await getStudentCourseCount()console.log("courseCount", courseCount)
};

测试结果:
在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com