解决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 =>{}
)
相关推荐
于慨15 小时前
Lambda 表达式、方法引用(Method Reference)语法
java·前端·servlet
石小石Orz15 小时前
油猴脚本实现生产环境加载本地qiankun子应用
前端·架构
从前慢丶15 小时前
前端交互规范(Web 端)
前端
@yanyu66615 小时前
07-引入element布局及spring boot完善后端
javascript·vue.js·spring boot
CHU72903515 小时前
便捷约玩,沉浸推理:线上剧本杀APP功能版块设计详解
前端·小程序
GISer_Jing15 小时前
Page-agent MCP结构
前端·人工智能
王霸天15 小时前
💥别再抄网上的Scale缩放代码了!50行源码教你写一个永不翻车的大屏适配
前端·vue.js·数据可视化
小领航15 小时前
用 Three.js + Vue 3 打造炫酷的 3D 行政地图可视化组件
前端·github
@大迁世界16 小时前
2026年React大洗牌:React Hooks 将迎来重大升级
前端·javascript·react.js·前端框架·ecmascript
PieroPc16 小时前
一个功能强大的 Web 端标签设计和打印工具,支持服务器端直接打印到局域网打印机。Fastapi + html
前端·html·fastapi