Interface package item summary data requested Vue

 

Interface package item summary data requested Vue

Recently I started the first job for the novice white front-end, before the usual practice is required to write directly on the page request interface data. But because the project will be frequent requests background, so all requests will be packaged together for easy management.

First, the configuration url (can be placed in a js)

  1. Introducing axios: import axios from "axios"
  2. Configuring url

                       const service=axios.create({

                                                 baseURL: the request address,

                                                 timeout: 5000 // access timeout limit

                      });

  1. Throws (if it is starting a new js file): export default service;

Second, write interface file (js file)

  1. The configuration url and axios introduced :( example configuration file called url request.js)

       2. Configure each interface for example and called business.js)

      Get request:

                     export function testGet (parameter) {

                             return request({

                                       method:’get’,

                                       url: 'Specific Interface ',

                                      params:{

                                             // json format, if you have to write, not to get rid of.

                                     }

                            })

                   }

     Post requests:

                    export function testPost (parameter) {

                                const data = {} // write parameters to be passed, JSON format

                                return request({

                                         method:’post,

                                        url: 'Specific Interface ',

                                       datatype:’json’,

                                       data

                               })

                    }

Third, the use

       1. introduced: import {name of the method to be introduced} from business.js

       2. Use

            . TestGet () then (res => {// into this success}) catch (err => {//} into this error).;

            On the method used in the current page.

Published 102 original articles · won praise 36 · views 110 000 +

Guess you like

Origin blog.csdn.net/wwf1225/article/details/103858565