Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useAxios.md
1---2category: '@Integrations'3---45# useAxios67Wrapper for [`axios`](https://github.com/axios/axios).89## Install1011```bash12npm i axios@^113```1415## Usage1617```ts18import { useAxios } from '@vueuse/integrations/useAxios'1920const { data, isFinished } = useAxios('/api/posts')21```2223### Return Values2425| Property | Type | Description |26| ------------------ | ---------------------------- | ---------------------------------------- |27| `data` | `Ref<T>` | Response data |28| `response` | `Ref<AxiosResponse>` | Full axios response |29| `error` | `Ref<unknown>` | Error if request failed |30| `isFinished` | `Ref<boolean>` | Request has completed (success or error) |31| `isLoading` | `Ref<boolean>` | Request is in progress |32| `isAborted` | `Ref<boolean>` | Request was aborted |33| `abort` / `cancel` | `() => void` | Abort the current request |34| `execute` | `(url?, config?) => Promise` | Execute/re-execute the request |3536### With Axios Instance3738```ts39import { useAxios } from '@vueuse/integrations/useAxios'40import axios from 'axios'4142const instance = axios.create({43baseURL: '/api',44})4546const { data, isFinished } = useAxios('/posts', instance)47```4849### With Config Options5051```ts52import { useAxios } from '@vueuse/integrations/useAxios'53import axios from 'axios'5455const instance = axios.create({56baseURL: '/api',57})5859const { data, isFinished } = useAxios('/posts', { method: 'POST' }, instance)60```6162### Manual Execution6364When you don't pass a `url`, the request won't fire immediately:6566```ts67import { useAxios } from '@vueuse/integrations/useAxios'6869const { execute } = useAxios()70execute(url)71```7273The `execute` function `url` is optional - `url2` will replace `url1`:7475```ts76import { useAxios } from '@vueuse/integrations/useAxios'7778const { execute } = useAxios(url1, {}, { immediate: false })79execute(url2)80```8182The `execute` function can accept config only:8384```ts85import { useAxios } from '@vueuse/integrations/useAxios'8687const { execute } = useAxios(url1, { method: 'GET' }, { immediate: false })88execute({ params: { key: 1 } })89execute({ params: { key: 2 } })90```9192### Awaiting Results9394The return value is thenable, so you can await it:9596```ts97import { useAxios } from '@vueuse/integrations/useAxios'9899const { data, isFinished, error } = await useAxios('/api/posts')100// data is now populated101```102103Or await the execute function:104105```ts106import { useAxios } from '@vueuse/integrations/useAxios'107108const { execute } = useAxios()109const result = await execute(url)110```111112### Options113114```ts115const { data } = useAxios('/api/posts', config, instance, {116// Execute immediately (default: true if url provided)117immediate: true,118// Use shallowRef for data (default: true)119shallow: true,120// Abort previous request on new execute (default: true)121abortPrevious: true,122// Reset data before executing (default: false)123resetOnExecute: false,124// Initial data value125initialData: [],126// Callbacks127onSuccess: data => console.log('Success:', data),128onError: error => console.error('Error:', error),129onFinish: () => console.log('Finished'),130})131```132133## Type Declarations134135```ts136export interface UseAxiosReturn<137T,138R = AxiosResponse<T>,139_D = any,140O extends UseAxiosOptions = UseAxiosOptions<T>,141> {142/**143* Axios Response144*/145response: ShallowRef<R | undefined>146/**147* Axios response data148*/149data: O extends UseAxiosOptionsWithInitialData<T>150? Ref<T>151: Ref<T | undefined>152/**153* Indicates if the request has finished154*/155isFinished: Ref<boolean>156/**157* Indicates if the request is currently loading158*/159isLoading: Ref<boolean>160/**161* Indicates if the request was canceled162*/163isAborted: Ref<boolean>164/**165* Any errors that may have occurred166*/167error: ShallowRef<unknown | undefined>168/**169* Aborts the current request170*/171abort: (message?: string | undefined) => void172/**173* Alias to `abort`174*/175cancel: (message?: string | undefined) => void176/**177* Alias to `isAborted`178*/179isCanceled: Ref<boolean>180}181export interface StrictUseAxiosReturn<182T,183R,184D,185O extends UseAxiosOptions = UseAxiosOptions<T>,186> extends UseAxiosReturn<T, R, D, O> {187/**188* Manually call the axios request189*/190execute: (191url?: string | AxiosRequestConfig<D>,192config?: AxiosRequestConfig<D>,193) => Promise<StrictUseAxiosReturn<T, R, D, O>>194}195export interface EasyUseAxiosReturn<T, R, D> extends UseAxiosReturn<T, R, D> {196/**197* Manually call the axios request198*/199execute: (200url: string,201config?: AxiosRequestConfig<D>,202) => Promise<EasyUseAxiosReturn<T, R, D>>203}204export interface UseAxiosOptionsBase<T = any> {205/**206* Will automatically run axios request when `useAxios` is used207*208*/209immediate?: boolean210/**211* Use shallowRef.212*213* @default true214*/215shallow?: boolean216/**217* Abort previous request when a new request is made.218*219* @default true220*/221abortPrevious?: boolean222/**223* Callback when error is caught.224*/225onError?: (e: unknown) => void226/**227* Callback when success is caught.228*/229onSuccess?: (data: T) => void230/**231* Sets the state to initialState before executing the promise.232*/233resetOnExecute?: boolean234/**235* Callback when request is finished.236*/237onFinish?: () => void238}239export interface UseAxiosOptionsWithInitialData<240T,241> extends UseAxiosOptionsBase<T> {242/**243* Initial data244*/245initialData: T246}247export type UseAxiosOptions<T = any> =248| UseAxiosOptionsBase<T>249| UseAxiosOptionsWithInitialData<T>250export declare function useAxios<251T = any,252R = AxiosResponse<T>,253D = any,254O extends UseAxiosOptionsWithInitialData<T> =255UseAxiosOptionsWithInitialData<T>,256>(257url: string,258config?: AxiosRequestConfig<D>,259options?: O,260): StrictUseAxiosReturn<T, R, D, O> & Promise<StrictUseAxiosReturn<T, R, D, O>>261export declare function useAxios<262T = any,263R = AxiosResponse<T>,264D = any,265O extends UseAxiosOptionsWithInitialData<T> =266UseAxiosOptionsWithInitialData<T>,267>(268url: string,269instance?: AxiosInstance,270options?: O,271): StrictUseAxiosReturn<T, R, D, O> & Promise<StrictUseAxiosReturn<T, R, D, O>>272export declare function useAxios<273T = any,274R = AxiosResponse<T>,275D = any,276O extends UseAxiosOptionsWithInitialData<T> =277UseAxiosOptionsWithInitialData<T>,278>(279url: string,280config: AxiosRequestConfig<D>,281instance: AxiosInstance,282options?: O,283): StrictUseAxiosReturn<T, R, D, O> & Promise<StrictUseAxiosReturn<T, R, D, O>>284export declare function useAxios<285T = any,286R = AxiosResponse<T>,287D = any,288O extends UseAxiosOptionsBase<T> = UseAxiosOptionsBase<T>,289>(290url: string,291config?: AxiosRequestConfig<D>,292options?: O,293): StrictUseAxiosReturn<T, R, D, O> & Promise<StrictUseAxiosReturn<T, R, D, O>>294export declare function useAxios<295T = any,296R = AxiosResponse<T>,297D = any,298O extends UseAxiosOptionsBase<T> = UseAxiosOptionsBase<T>,299>(300url: string,301instance?: AxiosInstance,302options?: O,303): StrictUseAxiosReturn<T, R, D, O> & Promise<StrictUseAxiosReturn<T, R, D, O>>304export declare function useAxios<305T = any,306R = AxiosResponse<T>,307D = any,308O extends UseAxiosOptionsBase<T> = UseAxiosOptionsBase<T>,309>(310url: string,311config: AxiosRequestConfig<D>,312instance: AxiosInstance,313options?: O,314): StrictUseAxiosReturn<T, R, D, O> & Promise<StrictUseAxiosReturn<T, R, D, O>>315export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(316config?: AxiosRequestConfig<D>,317): EasyUseAxiosReturn<T, R, D> & Promise<EasyUseAxiosReturn<T, R, D>>318export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(319instance?: AxiosInstance,320): EasyUseAxiosReturn<T, R, D> & Promise<EasyUseAxiosReturn<T, R, D>>321export declare function useAxios<T = any, R = AxiosResponse<T>, D = any>(322config?: AxiosRequestConfig<D>,323instance?: AxiosInstance,324): EasyUseAxiosReturn<T, R, D> & Promise<EasyUseAxiosReturn<T, R, D>>325```326