首页 资源列表 文章列表

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

uniapp项目,当时选择了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的插件

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


0.262841s