The usage of vue-axios-plugin, and quickly configure multiple interfaces for the project

In front-end development projects, data interaction with the back-end and requesting back-end data is an essential task. In the current front-end development work, the axios plug-in is usually used to get data from the back-end. Axios is a promise-based HTTP library that can be used in browsers and node.js. There are many advantages of axios, which will not be described here. Interested readers can search for them by themselves. If you use axios directly, for example:

npm install axios


yarn add axios

It is necessary to encapsulate the request and response of axios twice, and there will be more work. As a programmer, remember not to reinvent the wheel.

In the vue project, you can use the vue-axios-plugin plugin.

npm module import

First install via npm

npm install --save vue-axios-plugin


yarn add vue-axios-plugin

Then configure the entry file as follows:

import Vue from 'Vue'
import VueAxiosPlugin from 'vue-axios-plugin'

Vue.use(VueAxiosPlugin, {
  // Request interception processing
  reqHandleFunc: config => config,
  reqErrorFunc: error => Promise.reject(error),
  // Response interception processing
  resHandleFunc: response => response,
  resErrorFunc: error => Promise.reject(error)

Configuration parameters

In addition to the default provided by axios request configuration , vue-axios-plugin also provides request/response interceptor configuration, as follows:

parameter name type Defaults describe
reqHandleFunc {Function} config => config The interception handler before the request is initiated
reqErrorFunc {Function} error => Promise.reject(error) A function to handle request errors
resHandleFunc {Function} response => response Response data processing function
resErrorFunc {Function} error => Promise.reject(error) response error handler


Added the $http property to the Vue component, which provides get and post methods by default, which are used as follows:

this.$http.get(url, data, options).then((response) => {
this.$, data, options).then((response) => {

You can also use all axios api methods through this.$axios, as follows:

this.$axios.get(url, data, options).then((response) => {

this.$, data, options).then((response) => {

Each request method can pass in three parameters, the first parameter is the url multi-interface address, and the second parameter is the interface request parameter. So, what is the third parameter for?

If a project needs to configure multiple request interfaces, how to do it?

If you use the vue-axios-plugin plugin, it will be very convenient to solve this problem. At this time, you need to use the third parameter of the request method.

First of all, you must understand that the type of the third parameter is an object or an array (in doubt, research it yourself);


  const data = {
        phone: 'renlei',
        code: '12'
      const option = {
        interfaceType: 'first'
      this.$http.get('/cardLists', data, option).then(response => {
      const option1 = {
        interfaceType: 'second'
      this.$'/login', data, option1).then(response => {

Here I use get and post requests respectively, and both pass in the third parameter. The third parameter is an object. There is only one key-value pair in the object. The key interfaceType is the same, but the value is different.

Then use the third parameter passed in in the file that configures vue-axios-plugin, for example:


 // Request interception processing
  reqHandleFunc: config => {
    console.log(config.interfaceType === 'first');
    /* config.baseURL =
        process.env.NODE_ENV === 'production'
          ? ''
          : ''; */
    let url = '';
    if (config.interfaceType === 'first') {
      url = '';
    } else if (config.interfaceType === 'second') {
      url = '';
    config.baseURL = url;
    config.headers['Content-Type'] = 'application/json';
    config.headers.Accept = 'application/json';
    config.retry = 4;
    config.retryDelay = 1000;
    config.timeout = 60000;
    return config;



Tags: axios Vue

Posted by Rochtus on Wed, 04 May 2022 22:35:20 +0300