美文网首页
网络相关2024-06-15

网络相关2024-06-15

作者: iOS打怪升级 | 来源:发表于2024-06-14 17:00 被阅读0次

    常用的第三方网络库有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();
          })
    

    相关文章

      网友评论

          本文标题:网络相关2024-06-15

          本文链接:https://www.haomeiwen.com/subject/cedaqjtx.html