HarmonyOS 如何使用axios封装的请求实现取消请求功能

HarmonyOS
2025-01-10 08:34:03
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

暂不支持取消功能,可使用拦截器拦截请求。参考文档:https://gitee.com/openharmony-sig/ohos_axios#%E6%8B%A6%E6%88%AA%E5%99%A8

参考代码:

function axiosInter() {
  // 添加请求拦截器
  axios.interceptors.request.use((config:InternalAxiosRequestConfig) => {
    console.info("进入拦截器")
    let condition = true // 某条件
    // 对请求数据做点什么
    if (condition) {
      let error:ESObject = {code:'000'} as ESObject
      return Promise.reject(error);
    }
    return config;
  }, (error:AxiosError) => {
    // 对请求错误做些什么
    return Promise.reject(error);
  });
  // 添加响应拦截器
  axios.interceptors.response.use((response:AxiosResponse)=> {
    // 对响应数据做点什么
    return response;
  }, (error:AxiosError)=> {
    // 对响应错误做点什么
    return Promise.reject(error);
  });
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
分享
微博
QQ
微信
回复
2025-01-10 11:09:58
相关问题
HarmonyOS 如何取消未发送请求
522浏览 • 1回复 待解决
HarmonyOS 取消http请求
855浏览 • 1回复 待解决
HarmonyOS axios请求问题
535浏览 • 1回复 待解决
HarmonyOS 如何处理axios请求
619浏览 • 1回复 待解决
HarmonyOS http请求封装
685浏览 • 1回复 待解决
HarmonyOS http请求封装
640浏览 • 1回复 待解决
HarmonyOS rcp取消网络请求
726浏览 • 1回复 待解决
HarmonyOS axios接口请求
642浏览 • 1回复 待解决
webview如何实现网络请求拦截功能
2621浏览 • 1回复 待解决
HarmonyOS 如何取消一个HTTP请求
443浏览 • 1回复 待解决
HarmonyOS rcp通用请求Promise封装
789浏览 • 1回复 待解决
基于@ohos/axios网络请求能力
1138浏览 • 1回复 待解决
HarmonyOS http网络请求封装Demo
634浏览 • 1回复 待解决
HarmonyOS 关于rcp请求封装问题
570浏览 • 1回复 待解决