Data Fetching
const { data, error } = useRequest(Service, options);
This is the most basic API of VueRequest. The Service
must be a function that returns a Promise, and the result returned will determine whether to pass data
(resolve) or error
(reject). The input parameters of the function will be passed to the API interface as params
.
In other words, you can use a third-party request library (such as axios
) to obtain data, and then pass the request to VueRequest for management.
Let's take an example
import { useRequest } from 'vue-request';
import axios from 'axios';
const getUser = userName => {
return axios.get('api/user', {
params: {
name: userName,
},
});
};
const { data, run } = useRequest(getUser, {
defaultParams: ['attojs'],
});
// ...
run('vue-request');
Next, let's take a look at some other configurations.