Axios is an open source library that allows us to easily make HTTP requests. It’s effectively just fetch
with extra superpowers!
Axios是一个开放源代码库,使我们可以轻松地发出HTTP请求。 实际上,它是通过额外的超能力来fetch
!
Let’s see this in action by creating a new HTML5 project:
让我们通过创建一个新HTML5项目来了解这一点:
# Create directory with a name of your choosing
$ mkdir axios-js && cd axios-js
# Create files
$ touch index.html app.js
# Initialise a new npm project
$ npm init -y
# Install Axios
$ npm i axios -S
$ npm i parcel-bundler -D
# Open this up in your editor
$ code .
NOTE: Axios can also be added via a CDN like so: <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
注意:也可以通过CDN添加Axios,如下所示: <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
As you may be able to see from our npm install
calls, we’ll be using Parcel to bundle and serve our code. We can add an npm
script for this by heading over to package.json
:
从npm install
调用中您可能会看到,我们将使用Parcel捆绑并提供我们的代码。 我们可以通过转到package.json
来为此添加一个npm
脚本:
{
"scripts": {
"dev": "parcel index.html",
"build": "parcel build index.html"
}
}
As we’ll be using async
and await
inside of our project, we’ll install Babel and the @babel/polyfill
package:
因为我们将在项目中使用async
并await
,所以我们将安装Babel和@babel/polyfill
软件包:
$ npm i