解决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 =>{}
)
相关推荐
用户9385156350714 小时前
从 Prompt 到 Harness:AI 工程化的三年跃迁与实战解码
javascript·人工智能
小碗细面14 小时前
前端 Prompt 工程实战:如何搭建场景化 Prompt 库
前端·ai编程
阿瑞IT14 小时前
2026年 AI Agent 生产化落地全景:四大高频故障根因分析与工程解法
前端
木木剑光14 小时前
我开源了一个 React 组件库,沉淀了多个高频组件和实用 Hooks
前端·javascript·react.js
kyriewen14 小时前
DeepSeek API 高峰时段涨价 2 倍,便宜大碗的时代要结束了?
前端·ai编程·deepseek
Moment14 小时前
牛逼,NextJs 从 16.3 开始全面拥抱 Agent Native 🥰🥰🥰
前端·后端·面试
沸点小助手15 小时前
6月沸点活动获奖名单公示|本周互动话题上新🎊
前端·后端
Csvn15 小时前
React 19 `use()` 来了:以后数据加载可以不用 useEffect?
前端·react.js
没落英雄15 小时前
从零开始搭建一个 AI Agent —— LangChain + TypeScript 实战手记
前端·人工智能·架构
远航_15 小时前
git submodule
前端·后端·github