常用的第三方网络库有abner/axios
- 三个异步函数,最后同步回调更新UI : 采用Promise.all 方法
方法示例:
export const LCNetWorkInstancemAdMin = axios.create({
baseURL: 'https://www.域名.com',
timeout: 8000,
headers: {'X-Custom-Header': 'foobar'},
params: {'xxx':'xxxx'}
});
//模型定义
interface struct Person {
name: string;
age: number;
}
//参考方法
async getPersonList(classId: number): Promise<Person[]> {
try {
const response: AxiosResponse<CommonResponse<Person[]>> = await LCNetWorkInstancemAdMin.get<null, AxiosResponse<CommonResponse<Person[]>>, null>(API_LC.Goods_SiteApp.PersonList, {
params: {
"classId": classid,
}
})
return response.data.data as Person[]
} catch (e) {
return []
}
}
同步回调:
const scheduleListPromise = this.getPersonList();
const studyProgressPromise = this.bean.getStudyProgressForGoods();
const videoLastLogPromise = this.bean.getVideoLastLog();
Promise.all([scheduleListPromise, studyProgressPromise, videoLastLogPromise])
.then((results) => {
const scheduleList = results[0];
const studyProgress = results[1];
const videoLastLog = results[2];
this.categoryList = scheduleList;
this.studyProgress = studyProgress;
this.lastLearnLogModel = videoLastLog;
this.onAllRequestsComplete();
})