- If you don't know about alova yet, it is highly recommended that you read the alova overview first.
- Want to try it out first? You can click here to try a simple example!
Install
Alova combines the UI framework to make the request easier. You can use the use hook provided by alova to initiate a request, which will return stateful data related to multiple requests such as loading, and automatically manage it in alova them without having to maintain them yourself.
When using alova, please ensure that the UI framework meets the following version requirements:
- React: >= v16.8
- Vue: 2.7, 3.x
- Svelte: Any
Send a request using useRequest
First create an alova instance, use this instance to create the corresponding method, and pass it to useRequest.
- vue composition
- react
- svelte
- vue options
<template>
<div v-if="loading">Loading...</div>
<div v-else-if="error">{{ error. message }}</div>
<span v-else>responseData: {{ data }}</span>
</template>
<script setup>
import { createAlova, useRequest } from 'alova';
import GlobalFetch from 'alova/GlobalFetch';
import VueHook from 'alova/vue';
const alovaInstance = createAlova({
statesHook: VueHook,
requestAdapter: GlobalFetch(),
responded: response => response.json()
});
const { loading, data, error } = useRequest(
alovaInstance.Get('https://jsonplaceholder.typicode.com/todos/1')
);
</script>import { createAlova, useRequest } from 'alova';
import GlobalFetch from 'alova/GlobalFetch';
import ReactHook from 'alova/react';
const alovaInstance = createAlova({
statesHook: ReactHook,
requestAdapter: GlobalFetch(),
responded: response => response.json()
});
const App = () => {
const { loading, data, error } = useRequest(
alovaInstance.Get('https://jsonplaceholder.typicode.com/todos/1')
);
if (loading) {
return <div>Loading...</div>;
} else if (error) {
return <div>{error.message}</div>;
}
return (
<>
<span>responseData: {JSON.stringify(data)}</span>
</>
);
};
export default App;<script>
import { createAlova, useRequest } from 'alova';
import GlobalFetch from 'alova/GlobalFetch';
import SvelteHook from 'alova/svelte';
const alovaInstance = createAlova({
statesHook: SvelteHook,
requestAdapter: GlobalFetch(),
responded: response => response.json()
});
const { loading, data, error } = useRequest(alovaInstance.Get('https://jsonplaceholder.typicode.com/todos/1'));
</script>
{#if $loading}
<div>Loading...</div>
{:else if $error}
<div>{ $error.message }</div>
{:else}
<span>responseData: { data }</span>
{/if}
<template>
<div v-if="todo.loading">Loading...</div>
<div v-else-if="todo.error">{{ todo.error.message }}</div>
<span v-else>responseData: {{ todo.data }}</span>
</template>
<script>
import { createAlova, useRequest } from 'alova';
import GlobalFetch from 'alova/GlobalFetch';
import VueHook from 'alova/vue';
import { mapAlovaHook } from '@alova/vue-options';
const alovaInstance = createAlova({
statesHook: VueHook,
requestAdapter: GlobalFetch(),
responded: response => response.json()
});
export default {
mixins: mapAlovaHook(function () {
return {
todo: useRequest(
alovaInstance.Get('https://jsonplaceholder.typicode.com/todos/1')
)
}
}),
data() {
return {};
}
}
</script>
Use hook specification
Please note that the use of useRequest needs to conform to the rules of use hook, that is, it can only be called at the outermost layer of the function. ❌❌❌ It is not recommended to call in loops, conditional judgments or sub-functions.
For example, the following usage example in the click callback, when used in the callback function, although the request can be initiated normally, the responsive data returned by the use hook cannot be used in the view, and the same is true for loops and conditional judgments.
const handleClick = () => {
const { loading, data } = useRequest(getter);
};
const { loading, data, send } = useRequest(getter, {
immediate: false
});
const handleClick = () => {
send();
};
Use the method instance to send the request
The use hook can only be used to send requests within the component. Outside the component, you can directly send requests through the method instance.
const response = await alovaInstance.Get('https://api.alovajs.org/profile?id=1').send();
For more information about method instance sending request, please go to Use method instance to send request to read.
Regarding when to use useRequest to send a request and when to use a method instance to send a request, please read the Best Practice here.
Used in static html
In addition to using esModule to install alova, you can also use <script> tags to use alova.
- vue composition
- react
- svelte
- vue options
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script src="https://unpkg.com/alova/dist/alova.umd.min.js"></script>
<script src="https://unpkg.com/alova/dist/adapter/globalfetch.umd.min.js"></script>
<script src="https://unpkg.com/alova/dist/hooks/vuehook.umd.min.js"></script>
</head>
<body>
<div id="app">
<div v-if="loading">Loading...</div>
<div v-else-if="error">{{ error.message }}</div>
<span v-else>responseData: {{ data }}</span>
</div>
</body>
<script>
const alovaInstance = alova.createAlova({
statesHook: VueHook,
requestAdapter: GlobalFetch(),
responded: response => response.json()
});
Vue.createApp({
setup() {
return alova.useRequest(
alovaInstance.Get('https://jsonplaceholder.typicode.com/todos/1')
);
}
}).mount('#app');
</script>
</html><!DOCTYPE html>
<html lang="en">
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script src="https://unpkg.com/alova/dist/alova.umd.min.js"></script>
<script src="https://unpkg.com/alova/dist/adapter/globalfetch.umd.min.js"></script>
<script src="https://unpkg.com/alova/dist/hooks/reacthook.umd.min.js"></script>
</head>
<body>
<div id="app"></div>
</body>
<script type="text/babel">
const alovaInstance = alova.createAlova({
statesHook: ReactHook,
requestAdapter: GlobalFetch(),
responded: response => response.json()
});
const App = () => {
const { loading, data, error } = alova.useRequest(
alovaInstance.Get('https://jsonplaceholder.typicode.com/todos/1')
);
if (loading) {
return <div>Loading...</div>;
} else if (error) {
return <div>{error.message}</div>;
}
return (
<span>responseData: {JSON.stringify(data)}</span>
);
};
const root = ReactDOM.createRoot(document.getElementById('app'));
root.render(<App />);
</script>
</html>svelte depends on compilation tools and cannot be used directly through CDN. For details, see svelte.dev
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://unpkg.com/vue@2/dist/vue.js"></script>
<script src="https://unpkg.com/alova/dist/alova.umd.min.js"></script>
<script src="https://unpkg.com/alova/dist/adapter/globalfetch.umd.min.js"></script>
<script src="https://unpkg.com/@alova/vue-options/dist/alova-vue-options.umd.min.js"></script>
</head>
<body>
<div id="app">
<div v-if="todo.loading">Loading...</div>
<div v-else-if="todo.error">{{ todo.error.message }}</div>
<span v-else>responseData: {{ todo.data }}</span>
</div>
</body>
<script>
console.log(window)
const alovaInstance = alova.createAlova({
statesHook: AlovaVueOptions.VueOptionsHook,
requestAdapter: GlobalFetch(),
responded: response => response.json()
});
new Vue({
el: '#app',
mixins: AlovaVueOptions.mapAlovaHook(function () {
return {
todo: alova.useRequest(
alovaInstance.Get('https://jsonplaceholder.typicode.com/todos/1')
)
}
}),
data() {
return {};
}
});
</script>
</html>
What to do next?
In fact, this is just the simplest sample code, but alova also includes rich functions such as request and response interceptors, cache and state management, multiple core use hooks, etc., which we will elaborate in subsequent chapters.
If you want to better manage your request code, here is a best practice of method management, waiting for you to read it.