useAxios
Wrapper for axios
.
Demo
Available in the @vueuse/integrations add-on.Install
bash
npm i axios
npm i axios
Usage
ts
import { useAxios } from '@vueuse/integrations/useAxios'
const { data, isFinished } = useAxios('/api/posts')
import { useAxios } from '@vueuse/integrations/useAxios'
const { data, isFinished } = useAxios('/api/posts')
or use an instance of axios
ts
import axios from 'axios'
import { useAxios } from '@vueuse/integrations/useAxios'
const instance = axios.create({
baseURL: '/api',
})
const { data, isFinished } = useAxios('/posts', instance)
import axios from 'axios'
import { useAxios } from '@vueuse/integrations/useAxios'
const instance = axios.create({
baseURL: '/api',
})
const { data, isFinished } = useAxios('/posts', instance)
use an instance of axios with config options
ts
import axios from 'axios'
import { useAxios } from '@vueuse/integrations/useAxios'
const instance = axios.create({
baseURL: '/api',
})
const { data, isFinished } = useAxios('/posts', { method: 'POST' }, instance)
import axios from 'axios'
import { useAxios } from '@vueuse/integrations/useAxios'
const instance = axios.create({
baseURL: '/api',
})
const { data, isFinished } = useAxios('/posts', { method: 'POST' }, instance)
When you don't pass the url
. The default value is {immediate: false}
ts
import { useAxios } from '@vueuse/integrations/useAxios'
const { execute } = useAxios()
execute(url)
import { useAxios } from '@vueuse/integrations/useAxios'
const { execute } = useAxios()
execute(url)
The execute
function url
here is optional, and url2
will replace the url1
.
ts
import { useAxios } from '@vueuse/integrations/useAxios'
const { execute } = useAxios(url1, {}, { immediate: false })
execute(url2)
import { useAxios } from '@vueuse/integrations/useAxios'
const { execute } = useAxios(url1, {}, { immediate: false })
execute(url2)
The execute
function can accept config
only.
ts
import { useAxios } from '@vueuse/integrations/useAxios'
const { execute } = useAxios(url1, { method: 'GET' }, { immediate: false })
execute({ params: { key: 1 } })
execute({ params: { key: 2 } })
import { useAxios } from '@vueuse/integrations/useAxios'
const { execute } = useAxios(url1, { method: 'GET' }, { immediate: false })
execute({ params: { key: 1 } })
execute({ params: { key: 2 } })
The execute
function resolves with a result of network request.
ts
import { useAxios } from '@vueuse/integrations/useAxios'
const { execute } = useAxios()
const result = await execute(url)
import { useAxios } from '@vueuse/integrations/useAxios'
const { execute } = useAxios()
const result = await execute(url)
use an instance of axios with immediate
options
ts
import axios from 'axios'
import { useAxios } from '@vueuse/integrations/useAxios'
const instance = axios.create({
baseURL: '/api',
})
const { data, isFinished } = useAxios('/posts', { method: 'POST' }, instance, {
immediate: false,
})
import axios from 'axios'
import { useAxios } from '@vueuse/integrations/useAxios'
const instance = axios.create({
baseURL: '/api',
})
const { data, isFinished } = useAxios('/posts', { method: 'POST' }, instance, {
immediate: false,
})
Type Declarations
Show Type Declarations
typescript
export interface UseAxiosReturn<T, R = AxiosResponse<T>, D = any> {
/**
* Axios Response
*/
response: ShallowRef<R | undefined>
/**
* Axios response data
*/
data: Ref<T | undefined>
/**
* Indicates if the request has finished
*/
isFinished: Ref<boolean>
/**
* Indicates if the request is currently loading
*/
isLoading: Ref<boolean>
/**
* Indicates if the request was canceled
*/
isAborted: Ref<boolean>
/**
* Any errors that may have occurred
*/
error: ShallowRef<AxiosError<T, D> | undefined>
/**
* Aborts the current request
*/
abort: (message?: string | undefined) => void
/**
* isFinished alias
* @deprecated use `isFinished` instead
*/
finished: Ref<boolean>
/**
* isLoading alias
* @deprecated use `isLoading` instead
*/
loading: Ref<boolean>
/**
* isAborted alias
* @deprecated use `isAborted` instead
*/
aborted: Ref<boolean>
/**
* abort alias
*/
cancel: (message?: string | undefined) => void
/**
* isAborted alias
* @deprecated use `isCanceled` instead
*/
canceled: Ref<boolean>
/**
* isAborted alias
*/
isCanceled: Ref<boolean>
}
export interface StrictUseAxiosReturn<T, R, D> extends UseAxiosReturn<T, R, D> {
/**
* Manually call the axios request
*/
execute: (
url?: string | RawAxiosRequestConfig<D>,
config?: RawAxiosRequestConfig<D>
) => PromiseLike<StrictUseAxiosReturn<T, R, D>>
}
export interface EasyUseAxiosReturn<T, R, D> extends UseAxiosReturn<T, R, D> {
/**
* Manually call the axios request
*/
execute: (
url: string,
config?: RawAxiosRequestConfig<D>
) => PromiseLike<EasyUseAxiosReturn<T, R, D>>
}
export interface UseAxiosOptions<T = any> {
/**
* Will automatically run axios request when `useAxios` is used
*
*/
immediate?: boolean
/**
* Use shallowRef.
*
* @default true
*/
shallow?: boolean
/**
* Callback when error is caught.
*/
onError?: (e: unknown) => void
/**
* Callback when success is caught.
*/
onSuccess?: (data: T) => void
}
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(
url: string,
config?: RawAxiosRequestConfig<D>,
options?: UseAxiosOptions<T>
): StrictUseAxiosReturn<T, R, D> & PromiseLike<StrictUseAxiosReturn<T, R, D>>
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(
url: string,
instance?: AxiosInstance,
options?: UseAxiosOptions<T>
): StrictUseAxiosReturn<T, R, D> & PromiseLike<StrictUseAxiosReturn<T, R, D>>
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(
url: string,
config: RawAxiosRequestConfig<D>,
instance: AxiosInstance,
options?: UseAxiosOptions<T>
): StrictUseAxiosReturn<T, R, D> & PromiseLike<StrictUseAxiosReturn<T, R, D>>
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(
config?: RawAxiosRequestConfig<D>
): EasyUseAxiosReturn<T, R, D> & PromiseLike<EasyUseAxiosReturn<T, R, D>>
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(
instance?: AxiosInstance
): EasyUseAxiosReturn<T, R, D> & PromiseLike<EasyUseAxiosReturn<T, R, D>>
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(
config?: RawAxiosRequestConfig<D>,
instance?: AxiosInstance
): EasyUseAxiosReturn<T, R, D> & PromiseLike<EasyUseAxiosReturn<T, R, D>>
export interface UseAxiosReturn<T, R = AxiosResponse<T>, D = any> {
/**
* Axios Response
*/
response: ShallowRef<R | undefined>
/**
* Axios response data
*/
data: Ref<T | undefined>
/**
* Indicates if the request has finished
*/
isFinished: Ref<boolean>
/**
* Indicates if the request is currently loading
*/
isLoading: Ref<boolean>
/**
* Indicates if the request was canceled
*/
isAborted: Ref<boolean>
/**
* Any errors that may have occurred
*/
error: ShallowRef<AxiosError<T, D> | undefined>
/**
* Aborts the current request
*/
abort: (message?: string | undefined) => void
/**
* isFinished alias
* @deprecated use `isFinished` instead
*/
finished: Ref<boolean>
/**
* isLoading alias
* @deprecated use `isLoading` instead
*/
loading: Ref<boolean>
/**
* isAborted alias
* @deprecated use `isAborted` instead
*/
aborted: Ref<boolean>
/**
* abort alias
*/
cancel: (message?: string | undefined) => void
/**
* isAborted alias
* @deprecated use `isCanceled` instead
*/
canceled: Ref<boolean>
/**
* isAborted alias
*/
isCanceled: Ref<boolean>
}
export interface StrictUseAxiosReturn<T, R, D> extends UseAxiosReturn<T, R, D> {
/**
* Manually call the axios request
*/
execute: (
url?: string | RawAxiosRequestConfig<D>,
config?: RawAxiosRequestConfig<D>
) => PromiseLike<StrictUseAxiosReturn<T, R, D>>
}
export interface EasyUseAxiosReturn<T, R, D> extends UseAxiosReturn<T, R, D> {
/**
* Manually call the axios request
*/
execute: (
url: string,
config?: RawAxiosRequestConfig<D>
) => PromiseLike<EasyUseAxiosReturn<T, R, D>>
}
export interface UseAxiosOptions<T = any> {
/**
* Will automatically run axios request when `useAxios` is used
*
*/
immediate?: boolean
/**
* Use shallowRef.
*
* @default true
*/
shallow?: boolean
/**
* Callback when error is caught.
*/
onError?: (e: unknown) => void
/**
* Callback when success is caught.
*/
onSuccess?: (data: T) => void
}
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(
url: string,
config?: RawAxiosRequestConfig<D>,
options?: UseAxiosOptions<T>
): StrictUseAxiosReturn<T, R, D> & PromiseLike<StrictUseAxiosReturn<T, R, D>>
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(
url: string,
instance?: AxiosInstance,
options?: UseAxiosOptions<T>
): StrictUseAxiosReturn<T, R, D> & PromiseLike<StrictUseAxiosReturn<T, R, D>>
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(
url: string,
config: RawAxiosRequestConfig<D>,
instance: AxiosInstance,
options?: UseAxiosOptions<T>
): StrictUseAxiosReturn<T, R, D> & PromiseLike<StrictUseAxiosReturn<T, R, D>>
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(
config?: RawAxiosRequestConfig<D>
): EasyUseAxiosReturn<T, R, D> & PromiseLike<EasyUseAxiosReturn<T, R, D>>
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(
instance?: AxiosInstance
): EasyUseAxiosReturn<T, R, D> & PromiseLike<EasyUseAxiosReturn<T, R, D>>
export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(
config?: RawAxiosRequestConfig<D>,
instance?: AxiosInstance
): EasyUseAxiosReturn<T, R, D> & PromiseLike<EasyUseAxiosReturn<T, R, D>>
Source
Contributors
Anthony Fu
Jean-Baptiste AUBRÉE
azaleta
wheat
丶远方
jahnli
Jelf
马灿
lstoeferle
Marcos Dantas
Yiyang Sun
sun0day
vaakian X
flyingTodream
Curt Grimes
Jakub Freisler
Kasper Seweryn
webfansplz
WuLianN
unknown_
Manaus
Alex Kozack
Victor
Antério Vieira