适用于 Vue2 和 Vue3 的 uniapp Axios 适配器
在 uni-app 中,uni-network是一个体积性能更好的替代品
uni-network
npm i @uni-helper/axios-adapter axios
pnpm i @uni-helper/axios-adapter axios
bun add @uni-helper/axios-adapter axios
yarn add @uni-helper/axios-adapter axios
import { createUniAppAxiosAdapter } from '@uni-helper/axios-adapter' import axios from 'axios' axios.defaults.adapter = createUniAppAxiosAdapter()
或者创建自定义实例
import { createUniAppAxiosAdapter } from '@uni-helper/axios-adapter' import axios from 'axios' const instance = axios.create({ adapter: createUniAppAxiosAdapter() })
import { createUniAppAxiosAdapter } from '@uni-helper/axios-adapter' import axios from 'axios' axios.defaults.adapter = createUniAppAxiosAdapter() const { data, isFinished } = useAxios('/posts')
// 下载 axios.download('/') // or axios.request({ url: '/', method: 'download', }) // 上传 axios.upload('/', new File([new Blob()], 'fake file')) // or axios.request({ url: '/', method: 'upload', data: new File([new Blob()], 'fake file'), })
自 axios 1.4.0 开始,axios 内部已经处理了小程序的兼容性问题
FormData
Blob
自 1.4.0 开始,请始终保持主版本号和次版本号与 axios 一致。例如当你安装了 1.5.1 版本时,你可以安装 axios 的 1.5.x 版本。
这样做的目的是始终支持最新的 axios。
本页目录