解决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

下载

pnpm install @uni-helper/axios-adapter

使用

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

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 =>{}
)

uniapp使用axios,首先需要使用npm工具安装axios组件,并且指定版本。如果不指定版本,默认会安装最新版本。安装命令如下:`npm i axios@0.27.2`。接下来,需要创建并配置axios.js文件。在该文件中,我们需要解决uniapp适配axios请求时可能出现的报错"adapter is not a function"的问题。具体的解决方法如下所示: ```javascript import axios from 'axios' axios.defaults.adapter = config => { return new Promise((resolve, reject) => { let settle = require('axios/lib/core/settle'); let buildURL = require('axios/lib/helpers/buildURL'); uni.request({ method: config.method.toUpperCase(), url: config.baseURL + buildURL(config.url, config.params, config.paramsSerializer), header: config.headers, data: config.data, dataType: config.dataType, responseType: config.responseType, sslVerify: config.sslVerify, complete: function complete(response) { response = { data: response.data, status: response.statusCode, errMsg: response.errMsg, header: response.header, config: config }; settle(resolve, reject, response); } }) }) } ``` 最后,需要创建一个接口配置的js文件。完成以上工作后,就可以在uniapp使用axios进行网络请求了。可以通过测试来验证是否配置成功。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [第一章: uniapp引入axios异步框架](https://blog.csdn.net/qq_50661854/article/details/130222481)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [uniapp 使用axios](https://blog.csdn.net/wcdunf/article/details/125394205)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值