Skip to content
On this page

useAxios

Category
Package
@vueuse/integrations
Last Changed
a month ago

wrapper for axios

Demo

Loading: true
Finished: false
available in add-on @vueuse/integrations

Install

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

SourceDemoDocs

Contributors

Anthony Fu
wheat
lstoeferle
Marcos Dantas
unknown_
Manaus
Alex Kozack
Victor
Antério Vieira

Changelog

v7.5.5 on 1/25/2022
46752 - feat: added option to control whether the request fires immediately (#1170)
v5.0.0-beta.1 on 5/25/2021
5d19a - refactor!: remove deprecated apis
v4.10.0 on 5/12/2021
b09f1 - feat: bring API into line with useFetch (#499)
v4.10.0 on 5/12/2021
a05cb - feat: bring API into line with useFetch (#499)
v4.8.0 on 4/8/2021
7b855 - fix: use shallowRef for resposne, data, and error (#421)
v4.6.4 on 4/4/2021
3e14e - fix: disable cancel if request finished or not started (#412)
v4.6.3 on 3/31/2021
50e6e - fix: export UseAxiosReturn type (#396)
v4.5.0 on 3/25/2021
3a4cc - feat: new loading state in return object (#391)
useAxios has loaded