Skip to content

useAxios

类别
导出体积
1.56 kB
依赖包
@vueuse/integrations
上次更改
3 minutes ago

axios 的封装。

示例

Loading: true
Finished: false
Aborted: false
Available in the @vueuse/integrations add-on.

安装

bash
npm i axios@^1

用法

ts
import { useAxios } from '@vueuse/integrations/useAxios'

const { data, isFinished } = useAxios('/api/posts')

或者使用 axios 的实例

ts
import { useAxios } from '@vueuse/integrations/useAxios'
import axios from 'axios'

const instance = axios.create({
  baseURL: '/api',
})

const { data, isFinished } = useAxios('/posts', instance)

使用带有配置选项的 axios 实例

ts
import { useAxios } from '@vueuse/integrations/useAxios'
import axios from 'axios'

const instance = axios.create({
  baseURL: '/api',
})

const { data, isFinished } = useAxios('/posts', { method: 'POST' }, instance)

当你不传递 url 时,默认值是 { immediate: false }

ts
import { useAxios } from '@vueuse/integrations/useAxios'

const { execute } = useAxios()
execute(url)

这里 execute 函数的 url 是可选的,并且 url2 将替换 url1

ts
import { useAxios } from '@vueuse/integrations/useAxios'

const { execute } = useAxios(url1, {}, { immediate: false })
execute(url2)

execute 函数可以仅接受 config

ts
import { useAxios } from '@vueuse/integrations/useAxios'

const { execute } = useAxios(url1, { method: 'GET' }, { immediate: false })
execute({ params: { key: 1 } })
execute({ params: { key: 2 } })

execute 函数返回网络请求的结果。

ts
import { useAxios } from '@vueuse/integrations/useAxios'

const { execute } = useAxios()
const result = await execute(url)

使用带有 immediate 选项的 axios 实例

ts
import { useAxios } from '@vueuse/integrations/useAxios'
import axios from 'axios'

const instance = axios.create({
  baseURL: '/api',
})

const { data, isFinished } = useAxios('/posts', { method: 'POST' }, instance, {
  immediate: false,
})

类型声明

显示类型声明
typescript
export interface UseAxiosReturn<T, R = AxiosResponse<T>, _D = any> {
  /**
   * Axios 响应
   */
  response: ShallowRef<R | undefined>
  /**
   * Axios 响应数据
   */
  data: Ref<T | undefined>
  /**
   * 表示请求是否已完成
   */
  isFinished: Ref<boolean>
  /**
   * 表示请求是否当前正在加载
   */
  isLoading: Ref<boolean>
  /**
   * 表示请求是否已被取消
   */
  isAborted: Ref<boolean>
  /**
   * 可能发生的任何错误
   */
  error: ShallowRef<unknown | undefined>
  /**
   * 中止当前请求
   */
  abort: (message?: string | undefined) => void
  /**
   * `abort` 的别名
   */
  cancel: (message?: string | undefined) => void
  /**
   * `isAborted` 的别名
   */
  isCanceled: Ref<boolean>
}
export interface StrictUseAxiosReturn<T, R, D> extends UseAxiosReturn<T, R, D> {
  /**
   * 手动调用 axios 请求
   */
  execute: (
    url?: string | AxiosRequestConfig<D>,
    config?: AxiosRequestConfig<D>,
  ) => Promise<StrictUseAxiosReturn<T, R, D>>
}
export interface EasyUseAxiosReturn<T, R, D> extends UseAxiosReturn<T, R, D> {
  /**
   * 手动调用 axios 请求
   */
  execute: (
    url: string,
    config?: AxiosRequestConfig<D>,
  ) => Promise<EasyUseAxiosReturn<T, R, D>>
}
export interface UseAxiosOptions<T = any> {
  /**
   * 当使用 `useAxios` 时,是否自动运行 axios 请求
   */
  immediate?: boolean
  /**
   * 使用 shallowRef。
   *
   * @default true
   */
  shallow?: boolean
  /**
   * 当发起新请求时,中止前一个请求。
   *
   * @default true
   */
  abortPrevious?: boolean
  /**
   * 在捕获错误时的回调。
   */
  onError?: (e: unknown) => void
  /**
   * 在捕获成功时的回调。
   */
  onSuccess?: (data: T) => void
  /**
   * 初始数据
   */
  initialData?: T
  /**
   * 在执行 Promise 之前将状态设置为 initialState。
   */
  resetOnExecute?: boolean
  /**
   * 请求完成时的回调。
   */
  onFinish?: () => void
}
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(
  url: string,
  config?: AxiosRequestConfig<D>,
  options?: UseAxiosOptions,
): StrictUseAxiosReturn<T, R, D> & Promise<StrictUseAxiosReturn<T, R, D>>
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(
  url: string,
  instance?: AxiosInstance,
  options?: UseAxiosOptions,
): StrictUseAxiosReturn<T, R, D> & Promise<StrictUseAxiosReturn<T, R, D>>
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(
  url: string,
  config: AxiosRequestConfig<D>,
  instance: AxiosInstance,
  options?: UseAxiosOptions,
): StrictUseAxiosReturn<T, R, D> & Promise<StrictUseAxiosReturn<T, R, D>>
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(
  config?: AxiosRequestConfig<D>,
): EasyUseAxiosReturn<T, R, D> & Promise<EasyUseAxiosReturn<T, R, D>>
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(
  instance?: AxiosInstance,
): EasyUseAxiosReturn<T, R, D> & Promise<EasyUseAxiosReturn<T, R, D>>
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(
  config?: AxiosRequestConfig<D>,
  instance?: AxiosInstance,
): EasyUseAxiosReturn<T, R, D> & Promise<EasyUseAxiosReturn<T, R, D>>

源码

源码演示文档

贡献者

Anthony Fu
jahnli
一纸忘忧
丶远方
Jean-Baptiste AUBRÉE
azaleta
wheat
Anthony Fu
Jakub Freisler
Jelf
马灿
lstoeferle
Marcos Dantas
jdm1219
GeekTR
Doctorwu
ge Datou
Issayah
Mickaël Oth
Yiyang Sun
sun0day
vaakian X
flyingTodream
Curt Grimes
Kasper Seweryn
webfansplz
WuLianN
unknown_
Manaus
Alex Kozack
Victor
Antério Vieira

更新日志

v10.8.0 on 2/20/2024
b94de - feat: support abortPrevious option (#3735)
v10.7.2 on 1/14/2024
37eae - fix: ignore undefined options (#3662)
v10.7.0 on 12/5/2023
4b159 - fix: reset isAborted value on success (#3547)
v10.6.0 on 11/9/2023
151f9 - fix: bail out on request abort (#3394)
v10.2.0 on 6/16/2023
b1701 - fix: prevent premature loading refs reset (#3076)
v10.0.0-beta.5 on 4/13/2023
cb644 - refactor!: remove isFunction and isString utils
v10.0.0-beta.4 on 4/13/2023
edece - fix!: reject promise on execute (#2485)
f54a3 - feat: added initialData and resetOnExecute options (#2791)
v10.0.0-beta.3 on 4/12/2023
1f8b9 - fix!: remove deprecated apis
v10.0.0-beta.2 on 3/28/2023
a2f33 - feat: added onFinish callback (#2829)
v10.0.0-beta.0 on 3/14/2023
d8d73 - fix!: error should return type unknown (#2807)
v9.13.0 on 2/18/2023
7ad51 - fix: fix cancelToken (#2728)
809fc - feat: add success and error callbacks (#2714)
v9.11.0 on 1/17/2023
1e270 - fix: assign AxiosError to error.value when no url provided (close #2478) (#2484)
v9.3.1 on 10/17/2022
650fd - feat: add R genericity type for custom response data (#2304)
v9.3.0 on 9/26/2022
d065d - feat: add option for choosing shallowRef or ref (#2251)
c8c38 - feat: add second generic type to error (#2248)
f3ae7 - feat: improve type (#2208)