解决uniapp中使用axios在真机和模拟器下请求报错问题

由于我的uniapp项目是通过vite脚手架搭建的,当时选择了axios作为请求方式,在本地调试的时候也一直没发现有问题,直到打包成app在真机上登录,发现报错There is no suitable adapter to dispatch the request since:-adapter xhr is not supported by the environment -adapter http is not available in the build -adapter fetch is not supported by the environment;才发现uniapp并不兼容axios。为了解决这个问题,找到了一款axios适配uniapp的插件,使用方法也非常简单。

复制代码
@uni-helper/axios-adapter

插件地址:https://uni-helper.js.org/axios-adapter

下载

javascript 复制代码
pnpm install @uni-helper/axios-adapter

使用

在你的axios封装方法中,import导入插件,在axios.create里配置适配器adapter即可解决uniapp兼容axios问题。

javascript 复制代码
import axios from 'axios'
import { createUniAppAxiosAdapter } from '@uni-helper/axios-adapter'

// 配置服务api
const service = axios.create({ 
  baseURL: process.env.VUE_APP_BASE_API ,
  adapter:createUniAppAxiosAdapter(),
  timeout: 15000
})

//拦截
service.interceptors.request.use(
    config =>{},
    error =>{}
)

//响应
service.interceptors.response.use(
    response =>{},
    error =>{}
)
相关推荐
ZXT7 分钟前
promise & async await总结
前端
Jerry说前后端7 分钟前
RecyclerView 性能优化:从原理到实践的深度优化方案
android·前端·性能优化
画个太阳作晴天15 分钟前
A12预装app
linux·服务器·前端
77238941 分钟前
解决 Microsoft Edge 显示“由你的组织管理”问题
前端·microsoft·edge
烛阴1 小时前
前端必会:如何创建一个可随时取消的定时器
前端·javascript·typescript
JarvanMo1 小时前
Swift 应用在安卓系统上会怎么样?
前端
LinXunFeng2 小时前
Flutter - 详情页 TabBar 与模块联动?秒了!
前端·flutter·开源
萌萌哒草头将军2 小时前
Oxc 最新 Transformer Alpha 功能速览! 🚀🚀🚀
前端·javascript·vue.js
Justinc.2 小时前
HTML5新增属性
前端·html·html5
1024小神3 小时前
nextjs项目build导出静态文件
前端·javascript