Skip to main content
Version: v2

alova instance

createAlova()

Create an alova instance.

  • type
function createAlova(options?: AlovaOptions): Alova;
  • Parameters
  1. config: configuration parameters
Parameter nameTypeDescription
baseURLstringBase path, empty by default, View details
statesHookobjectState management hook, optional, View details
requestAdapterobjectRequest adapter, required, View details
timeoutnumberTimeout time, no timeout by default, View details
localCacheobjectLocal cache configuration, default GET has 5000ms cache, View details
storageAdapterobjectLocal storage adapter, default is localStorage, View details
beforeRequestfunctionBefore request hook, View details
respondedobject | functionRequest response hook, View details
shareRequestbooleanShare request, View details
errorLoggerboolean| null | functionError log, View details
cacheLoggerboolean | null | functionCache log, View details
  • return

Alova instance

  • Example
import { createAlova } from 'alova';

const alova = createAlova({
baseURL: 'https://example.com',
statesHook: VueHook,
requestAdapter: GlobalFetch(),
timeout: 3000
// ...
});

alova.id

The alova instance id is used to distinguish different alova instances. It can accurately match the method instance of the specified alova in method instance matcher.

  • Type: string

alova.options

When creating an alova instance through createAlova, the default configuration is merged with the passed-in configuration object.

  • type
interface AlovaOptions {
statesHook: StatesHook;
requestAdapter: AlovaRequestAdapter;
baseURL?: string;
timeout?: number;
localCache?: GlobalLocalCacheConfig;
storageAdapter?: AlovaStorageAdapter;
beforeRequest?: Function;
responded?: Function | ResponsedHandlerRecord;
shareRequest?: boolean;
errorLogger?: boolean | null | Function;
cacheLogger?: boolean | null | Function;
}

alova.storage

The storage adapter instance corresponding to the alova instance defaults to AlovaGlobalStorage, which uses localStorage.

  • type
interface AlovaStorageAdapter {
get(key: string): any;
set(key: string, value: any): void;
remove(key: string): void;
}

alova.Get()

Create a method instance for the GET request.

  • type
interface Alova {
Get(url: string, config?: AlovaMethodCreateConfig): Method;
}
  • Parameters
  1. url: request address
  2. config: configuration parameters
Parameter nameTypeDescription
headersobjectRequest headers, View details
paramsobjectRequest parameters, View details
namestringmethod object name, in updateState, invalidateCache, setCache, and fetch function, you can obtain the corresponding method instance by name or wildcard
timeoutnumberRequest timeout, View details
localCacheLocalCacheConfigResponse cache time, View details
hitSourcestringHit the source method instance. When the source method instance request is successful, the cache of the current method instance will be invalidated. View details
enableDownloadbooleanEnable download progress information, View details
enableUploadbooleanEnable upload progress information, View details
transformDatafunctionTransform response data, View details
shareRequestbooleanRequest-level sharing request switch, View details

In addition to the configurable parameters above, other parameters supported by the request adapter are also supported.

  • return

method instance

  • Example
const getUsers = alovaInstance.Get('/users', {
params: {
ID: 1
}
// ...
});

alova.Post()

Create a method instance for the POST request.

  • type
interface Alova {
Post(
url: string,
data?: object | FormData | string | null,
config?: AlovaMethodCreateConfig
): Method;
}
  • Parameters
  1. url: request address
  2. data: request body
  3. config: configuration parameters, the parameter type is the same as alova.Get
  • return

method instance

  • Example
const postUsers = alovaInstance.Post(
'/createUser',
{
name: 'alova',
age: 18,
gender: 'male'
},
{
// Configuration parameters...
}
);

alova.Delete()

Create a method instance for the DELETE request.

  • type
interface Alova {
Delete(
url: string,
data?: object | FormData | string | null,
config?: AlovaMethodCreateConfig
): Method;
}
  • Parameters
  1. url: request address
  2. data: request body
  3. config: configuration parameters, the parameter type is the same as alova.Get
  • return

method instance

  • Example
const deleteUsers = alovaInstance.Delete(
'/deleteUser',
{
ID: 1
},
{
// Configuration parameters...
}
);

alova.Put()

Create a method instance for the PUT request.

  • type
interface Alova {
Put(
url: string,
data?: object | FormData | string | null,
config?: AlovaMethodCreateConfig
): Method;
}
  • Parameters
  1. url: request address
  2. data: request body
  3. config: configuration parameters, the parameter type is the same as alova.Get
  • return

method instance

  • Example
const putUsers = alovaInstance.Put(
'/updateUser',
{
id: 1,
name: 'alova'
},
{
// Configuration parameters...
}
);

alova.Head()

Create a method instance of the HEAD request.

  • type
interface Alova {
Head(url: string, config?: AlovaMethodCreateConfig): Method;
}
  • Parameters
  1. url: request address
  2. config: configuration parameters, the parameter type is the same as alova.Get
  • return

method instance

alova.Patch()

Create a method instance for the PATCH request.

  • type
interface Alova {
Patch(
url: string,
data?: object | FormData | string | null,
config?: AlovaMethodCreateConfig
): Method;
}
  • Parameters
  1. url: request address
  2. data: request body
  3. config: configuration parameters, the parameter type is the same as alova.Get
  • return

method instance

alova.Options()

Create a method instance of the OPTIONS request.

  • type
interface Alova {
Options(url: string, config?: AlovaMethodCreateConfig): Method;
}
  • Parameters
  1. url: request address
  2. config: configuration parameters, the parameter type is the same as alova.Get
  • return

method instance