使用场景:在添加学生上课记录的时候,需要先获取学生的剩余课时,需要通过接口获取。所以需要封装一个方法,能够通过接口获取学生的课时数量。
解决方案:通过异步解决
封装方法的代码如下:
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)
};
测试结果: