Data Fetching

const { data, error } = useRequest(Service, options);

This is the very fundamental API of VueRequest. The Service here must be a function that returns a Promise, and the returned result will decide 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 third-party request library (such as axios) to get data and then pass the request to VueRequest for management. For details, please refer to the following 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.

Last Updated: 7/1/2022, 8:28:34 AM
Contributors: John