Several conventional methods axios

      Vue is recommended axios a network request: Vue project recently started again, return to it. Several methods axios from the beginning of it.

      1. Installation: Since it is a Vue project, I chose common way npm

        $ npm install axios  (-d)

  2. Introduction: const axios = require ( 'axios') or transformed with ESmodule babel manner const axios = import ( 'axios')

  3. Request: GET request, the request method is very flexible:

    axios.get ( '/ user? ID = 12345') ---------------------- URL parameter carries a way

    axios.get ( '/ user', {params: {ID: 12345}}) ------- mode parameter object

    Note: Returns are processed promise ------ Here are two ways to return

    .. A Promise.then () catch in the form of:

      axios.get('/user',{param:{ID:12345}}).then(response => {console.log(response)})

    b. async function getUser() {

      try {

        const response = await axios.get('/user',{ param:{ ID:12345} })

      } catch (e) { 

        console.log(e)

      }

     }    

    Post Request: parameter object passed embodiment according to

      axios.post ( '/ user', {name: "Joe Smith", age: "18"}) Note: param there is no direct way to pass the object on it.

 

  4. Concurrent Request 

    fucntion getUser(){

     axios.get('/user/1234')

            }

    fucntion getPermission (){

     axios.get('/user/1234/permissions')

            }

   axios.all ([getUser, getPermission]). then (axios.spread (function (acct, perms) {----. then method, call axios.spread method parameter function, which accepts two parameters acct pe

      // These two parameters, respectively, two return request.

    })) ------- Promise i.e. an array of sources of error function capturing catch, to be investigated ---

 

  The basic configuration is provided comprising a head and other commonly used baseUrl baseUrl axios.creat ({config} objects)

    axios.creat ({

      baseURL: 'http: //some-domain.com/api/', // url basis to determine the configuration of the operating environment by webpack, you can change the online address, the address prefix development

      timeout: 1000, // response timeout

      headers:{Content-Type:application/x-www-form-urlencoded }  // 请求头

    })

Guess you like

Origin www.cnblogs.com/yaya666/p/12128727.html