关于无法在uniapp项目开发中使用axios的解决方案

问题描述

首先需要指出的是uniapp官方提供了uni.request方法来进行网络请求访问(还是建议使用这个),不过如果像往常项目编写中继续使用axios的话就会出现以下错误:

{ "message": "There is no suitable adapter to dispatch the request since :\n- adapter xhr is not supported by the environment\n- adapter http is not available in the build", "name": "AxiosError", "stack": "AxiosError: There is no suitable adapter to dispatch the request since :\n- adapter xhr is not supported by the environment\n- adapter http is not available in the build\n at Object.getAdapter (app-service.js:1674:15)\n at Axios.dispatchRequest (app-service.js:1701:30)\n at Axios._request (app-service.js:1978:35)\n at Axios.request (app-service.js:1882:27)\n at Axios.<computed> [as get] (app-service.js:1997:19)\n at Function.wrap [as get] (app-service.js:192:17)\n at getPic (app-service.js:2454:23)\n at app-service.js:2464:9\n at photo (app-service.js:84:13)\n at Array.forEach (<anonymous>)\n at Axios.request (app-service.js:1886:43)\n at async getPic (app-service.js:2454:9)", "code": "ERR_NOT_SUPPORT", "status": null }

解决方案

“There is no suitable adapter to dispatch the request”根据这个报错内容,可以看出,我们需要配置一个axios适配器

先向项目中添加模块(npm install)

yarn add axios-adapter-uniapp

然后回到我们的项目中

import axios from 'axios'
import axiosAdapterUniapp from 'axios-adapter-uniapp'
 
const instance = axios.create({
  baseURL: 'URL.com',
  adapter: axiosAdapterUniapp
})

在完成了以上配置后,我们就可以像以前一样使用instance来进行网络请求了

instance.get(“请求地址”)…

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值