axios的安装和使用

1.安装axios

npm install axios

2.引用axios
import axios from 'axios'

3.发送get请求 参考地址 GitHub - axios/axios: Promise based HTTP client for the browser and node.js
// Make a request for a user with a given ID
axios.get('/user?ID=12345')
  .then(function (response) {
    // handle success
    console.log(response);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  })
  .then(function () {
    // always executed
  });

// Optionally the request above could also be done as
axios.get('/user', {
    params: {
      ID: 12345
    }
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  })
  .then(function () {
    // always executed
  });  
// GET request for remote image in node.js
axios({
  method: 'get',
  url: 'http://bit.ly/2mTM3nY',
  responseType: 'stream'
})
  .then(function (response) {
    response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
  });


// Want to use async/await? Add the `async` keyword to your outer function/method.
async function getUser() 
{
try { const response = await axios.get('/user?ID=12345'); console.log(response); } catch (error) { console.error(error); } }
4.发送post请求
axios.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
原文地址:https://www.cnblogs.com/kingsmart/p/14932099.html