useAxios
wrapper for axios
Demo
available in add-on @vueuse/integrationsInstall
npm i axios
npm i axios
Usage
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
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
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)
Type Declarations
Show Type Declarations
export interface UseAxiosReturn<T> {
/**
* Axios Response
*/
response: Ref<AxiosResponse<T> | 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
*/
aborted: Ref<boolean>
/**
* Any errors that may have occurred
*/
error: Ref<AxiosError<T> | undefined>
/**
* Aborts the current request
*/
abort: (message?: string | undefined) => void
/**
* Manually call the axios request
*/
execute: (config?: AxiosRequestConfig) => void
}
export interface UseAxiosOptions {
/**
* Will automatically run axios request when `useAxios` is used
*
* @default true
*/
immediate?: boolean
}
export declare function useAxios<T = any>(
url: string,
config?: AxiosRequestConfig,
options?: UseAxiosOptions
): UseAxiosReturn<T>
export declare function useAxios<T = any>(
url: string,
instance?: AxiosInstance,
options?: UseAxiosOptions
): UseAxiosReturn<T>
export declare function useAxios<T = any>(
url: string,
config: AxiosRequestConfig,
instance: AxiosInstance,
options?: UseAxiosOptions
): UseAxiosReturn<T>
export interface UseAxiosReturn<T> {
/**
* Axios Response
*/
response: Ref<AxiosResponse<T> | 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
*/
aborted: Ref<boolean>
/**
* Any errors that may have occurred
*/
error: Ref<AxiosError<T> | undefined>
/**
* Aborts the current request
*/
abort: (message?: string | undefined) => void
/**
* Manually call the axios request
*/
execute: (config?: AxiosRequestConfig) => void
}
export interface UseAxiosOptions {
/**
* Will automatically run axios request when `useAxios` is used
*
* @default true
*/
immediate?: boolean
}
export declare function useAxios<T = any>(
url: string,
config?: AxiosRequestConfig,
options?: UseAxiosOptions
): UseAxiosReturn<T>
export declare function useAxios<T = any>(
url: string,
instance?: AxiosInstance,
options?: UseAxiosOptions
): UseAxiosReturn<T>
export declare function useAxios<T = any>(
url: string,
config: AxiosRequestConfig,
instance: AxiosInstance,
options?: UseAxiosOptions
): UseAxiosReturn<T>
Source
Contributors
Anthony Fu
wheat
lstoeferle
Marcos Dantas
unknown_
Manaus
Alex Kozack
Victor
Antério Vieira