安装引入axios

  • Post author:
  • Post category:其他



安装axios

npm install axios
$ bower install axios


cdn引入:

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>


引入

import axios from "axios" //导入axios


例子

const axios = require('axios');

// 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
  });  

// 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);
  }
}



版权声明:本文为qq_45807943原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。