English | 简体中文
- 🦾 Based on vue-demi, useAxios written by compositionApi supports vue2/vue3.
# npm
npm i demi-axios
# yarn
yarn add demi-axios
# pnpm
pnpm i demi-axios
// init
import { create } from 'demi-axios'
import type { AxiosInstance } from 'axios'
const axios: AxiosInstance = create({/* axiosOptions */})
// axios.interceptors.response.use((response) => {
// ...
// return response
// }, (error) => {
// return Promise.reject(error)
// })
// Usage
// use***(initialData, url, Formatter(responseData, currentData))
import { useGet } from 'demi-axios'
const { data, loading, task, error, response } = useGet(
{},
'https://jsonplaceholder.typicode.com/posts/1',
(responseData, currentData)=>{
// process
return responseData
}
)
// data : Ref<T> Return value
// loading : Ref<boolean>
// task(playload, config)
// error : Ref<T>
// response : Ref<T>
// example 1
task({params: 'demi'}).then((res) => {
console.log(res)
})
// example 2
await task()
console.log(data.value)
consoe.log(response.value)
useGet
useHead
useDelete
useOptions
useGetBlob
useHeadBlob
useDeleteBlob
useOptionsBlob
usePost
usePut
usePatch
usePostEncoded
usePutEncoded
usePatchEncoded
usePostMultipart usePutMultipart usePatchMultipart
FAQ
1. data.value is undefined
The default data format returned by the server is {data:any}. If you are {}, you can do a layer of data processing in the interceptor.
axios.interceptors.response.use((response) => {
// ...
return { data:response }
}, (error) => {
return Promise.reject(error)
})