<h1 align="center">
<b>
<a href="https://axios-http.com"><img src="https://axios-http.com/assets/logo.svg" /></a><br>
</b>
</h1>
<p align="center">Promise based HTTP client for the browser and node.js</p>
<p align="center">
<a href="https://axios-http.com/"><b>Website</b></a> â¢
<a href="https://axios-http.com/docs/intro"><b>Documentation</b></a>
</p>
<div align="center">
[![npm version](https://img.shields.io/npm/v/axios.svg?style=flat-square)](https://www.npmjs.org/package/axios)
[![CDNJS](https://img.shields.io/cdnjs/v/axios.svg?style=flat-square)](https://cdnjs.com/libraries/axios)
[![Build status](https://img.shields.io/github/actions/workflow/status/axios/axios/ci.yml?branch=v1.x&label=CI&logo=github&style=flat-square)](https://github.com/axios/axios/actions/workflows/ci.yml)
[![Gitpod Ready-to-Code](https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod&style=flat-square)](https://gitpod.io/#https://github.com/axios/axios)
[![code coverage](https://img.shields.io/coveralls/mzabriskie/axios.svg?style=flat-square)](https://coveralls.io/r/mzabriskie/axios)
[![install size](https://img.shields.io/badge/dynamic/json?url=https://packagephobia.com/v2/api.json?p=axios&query=$.install.pretty&label=install%20size&style=flat-square)](https://packagephobia.now.sh/result?p=axios)
[![npm bundle size](https://img.shields.io/bundlephobia/minzip/axios?style=flat-square)](https://bundlephobia.com/package/axios@latest)
[![npm downloads](https://img.shields.io/npm/dm/axios.svg?style=flat-square)](https://npm-stat.com/charts.html?package=axios)
[![gitter chat](https://img.shields.io/gitter/room/mzabriskie/axios.svg?style=flat-square)](https://gitter.im/mzabriskie/axios)
[![code helpers](https://www.codetriage.com/axios/axios/badges/users.svg)](https://www.codetriage.com/axios/axios)
[![Known Vulnerabilities](https://snyk.io/test/npm/axios/badge.svg)](https://snyk.io/test/npm/axios)
</div>
## Table of Contents
- [Features](#features)
- [Browser Support](#browser-support)
- [Installing](#installing)
- [Package manager](#package-manager)
- [CDN](#cdn)
- [Example](#example)
- [Axios API](#axios-api)
- [Request method aliases](#request-method-aliases)
- [Concurrency ð](#concurrency-deprecated)
- [Creating an instance](#creating-an-instance)
- [Instance methods](#instance-methods)
- [Request Config](#request-config)
- [Response Schema](#response-schema)
- [Config Defaults](#config-defaults)
- [Global axios defaults](#global-axios-defaults)
- [Custom instance defaults](#custom-instance-defaults)
- [Config order of precedence](#config-order-of-precedence)
- [Interceptors](#interceptors)
- [Multiple Interceptors](#multiple-interceptors)
- [Handling Errors](#handling-errors)
- [Cancellation](#cancellation)
- [AbortController](#abortcontroller)
- [CancelToken ð](#canceltoken-deprecated)
- [Using application/x-www-form-urlencoded format](#using-applicationx-www-form-urlencoded-format)
- [URLSearchParams](#urlsearchparams)
- [Query string](#query-string-older-browsers)
- [ð Automatic serialization](#-automatic-serialization-to-urlsearchparams)
- [Using multipart/form-data format](#using-multipartform-data-format)
- [FormData](#formdata)
- [ð Automatic serialization](#-automatic-serialization-to-formdata)
- [Files Posting](#files-posting)
- [HTML Form Posting](#-html-form-posting-browser)
- [ð Progress capturing](#-progress-capturing)
- [ð Rate limiting](#-progress-capturing)
- [ð AxiosHeaders](#-axiosheaders)
- [Semver](#semver)
- [Promises](#promises)
- [TypeScript](#typescript)
- [Resources](#resources)
- [Credits](#credits)
- [License](#license)
## Features
- Make [XMLHttpRequests](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) from the browser
- Make [http](https://nodejs.org/api/http.html) requests from node.js
- Supports the [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) API
- Intercept request and response
- Transform request and response data
- Cancel requests
- Automatic transforms for [JSON](https://www.json.org/json-en.html) data
- ð Automatic data object serialization to `multipart/form-data` and `x-www-form-urlencoded` body encodings
- Client side support for protecting against [XSRF](https://en.wikipedia.org/wiki/Cross-site_request_forgery)
## Browser Support
![Chrome](https://raw.githubusercontent.com/alrra/browser-logos/main/src/chrome/chrome_48x48.png) | ![Firefox](https://raw.githubusercontent.com/alrra/browser-logos/main/src/firefox/firefox_48x48.png) | ![Safari](https://raw.githubusercontent.com/alrra/browser-logos/main/src/safari/safari_48x48.png) | ![Opera](https://raw.githubusercontent.com/alrra/browser-logos/main/src/opera/opera_48x48.png) | ![Edge](https://raw.githubusercontent.com/alrra/browser-logos/main/src/edge/edge_48x48.png) | ![IE](https://raw.githubusercontent.com/alrra/browser-logos/master/src/archive/internet-explorer_9-11/internet-explorer_9-11_48x48.png) |
--- | --- | --- | --- | --- | --- |
Latest â | Latest â | Latest â | Latest â | Latest â | 11 â |
[![Browser Matrix](https://saucelabs.com/open_sauce/build_matrix/axios.svg)](https://saucelabs.com/u/axios)
## Installing
### Package manager
Using npm:
```bash
$ npm install axios
```
Using bower:
```bash
$ bower install axios
```
Using yarn:
```bash
$ yarn add axios
```
Using pnpm:
```bash
$ pnpm add axios
```
Once the package is installed, you can import the library using `import` or `require` approach:
```js
import axios, {isCancel, AxiosError} from 'axios';
```
You can also use the default export, since the named export is just a re-export from the Axios factory:
```js
import axios from 'axios';
console.log(axios.isCancel('something'));
````
If you use `require` for importing, **only default export is available**:
```js
const axios = require('axios');
console.log(axios.isCancel('something'));
```
For cases where something went wrong when trying to import a module into a custom or legacy environment,
you can try importing the module package directly:
```js
const axios = require('axios/dist/browser/axios.cjs'); // browser commonJS bundle (ES2017)
// const axios = require('axios/dist/node/axios.cjs'); // node commonJS bundle (ES2017)
```
### CDN
Using jsDelivr CDN (ES5 UMD browser module):
```html
<script src="https://cdn.jsdelivr.net/npm/axios@1.1.2/dist/axios.min.js"></script>
```
Using unpkg CDN:
```html
<script src="https://unpkg.com/axios@1.1.2/dist/axios.min.js"></script>
```
## Example
> **Note**: CommonJS usage
> In order to gain the TypeScript typings (for intellisense / autocomplete) while using CommonJS imports with `require()`, use the following approach:
```js
import axios from 'axios';
//const axios = require('axios'); // legacy way
// 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);
})
.finally(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);
})
.finally(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);
}
}
```
> **Note**: `async/await` is part of ECMAScript 2017 and is not supported in Internet
> Explorer and older browsers, so use with caution.
Performing a `POST` request
```js
axios.post('/user', {
firstName: 'Fred',
lastName:
vue3+ts的基础学习
需积分: 0 143 浏览量
更新于2023-12-04
收藏 12.14MB ZIP 举报
Vue3 是 Vue.js 框架的最新版本,它带来了许多改进和新特性,使得开发更加高效和可维护。TypeScript(简称TS)是 JavaScript 的一个超集,它提供了静态类型检查,增强了代码的可预测性和可维护性。在 Vue3 中结合 TypeScript 使用,可以显著提升开发体验和项目的质量。
1. **setup 与 setup() 数据类型**:
在 Vue2 中,我们通常使用 `data`、`methods` 和 `computed` 等选项来定义组件的状态和行为。但在 Vue3 中,`setup` 函数成为了一个新的入口点,它是组合式 API 的核心。`setup` 函数允许开发者在组件实例创建之前访问到 `props` 和 `context`。由于 Vue3 支持 TypeScript,因此在 `setup` 中可以使用 `ref` 和 `reactive` 创建响应式数据,并声明它们的类型,提高代码的可读性和安全性。
2. **函数**:
在 Vue3 的 `setup` 中,我们可以定义函数来处理逻辑,这些函数可以通过 `return` 导出并供组件模板使用。Vue3 引入了新的函数调用语法,如 `onMounted`、`onBeforeUpdate` 等生命周期钩子,它们在特定的组件生命周期阶段执行,有助于实现更清晰的代码结构。
3. **路由跳转**:
Vue3 依然兼容 Vue Router,一个强大的前端路由库。在 Vue3 中,我们可以在 `setup` 中使用 `import` 导入 `router` 实例,然后通过 `router.push` 或 `router.replace` 进行路由跳转。同时,Vue3 支持通过 `useRoute` 和 `useRouter` composable 函数来访问当前路由信息和路由实例,使得路由操作更加灵活。
4. **props 与 emit**:
Vue3 对 `props` 的传递和验证进行了优化。现在可以使用 `defineProps` 函数定义 props 的类型,这在 TypeScript 中尤其有用。同时,`props` 的默认值可以通过对象字面量直接设置。对于事件触发,Vue3 引入了 `emit` 函数,用于在组件内部触发父组件监听的事件。使用 `defineEmits` 可以在组件定义时声明允许发出的事件及其参数类型,提高了代码的类型安全。
5. **Composition API**:
Vue3 的一大亮点是引入了 Composition API,它允许开发者将功能分解为可重用的模块。例如,状态管理可以通过 `ref`、`reactive` 和 `toRefs` 来实现,而 `watch` 和 `watchEffect` 用于监听数据变化。这种模式使得代码更易于理解和测试。
6. **Suspense 组件**:
Vue3 添加了 `Suspense` 组件,用于在组件渲染完成前显示一个占位符,这对于优化用户体验和管理异步加载的组件非常有用。
7. **Teleport**:
新的 `Teleport` 组件允许将部分内容渲染到文档的其他位置,这在处理如模态框、 toast 通知等特殊场景时特别有用。
8. **Fragment 和 Slot Props**:
Vue3 支持片段(Fragment)作为根元素,解决了 Vue2 中单个根节点的限制。同时,增加了 slot props 功能,允许 slot 内容接收组件的 props。
9. **优化与性能提升**:
Vue3 提供了更好的性能,包括更快的更新机制、更小的体积以及对 Tree-shaking 的优化,使得在生产环境中的应用更为轻量且高效。
通过学习 Vue3 和 TypeScript 的结合,开发者不仅可以掌握最新的前端技术,还能编写出更加健壮、易于维护的应用程序。在这个过程中,理解并熟练运用 `setup`、`props`、`emit`、路由跳转和 Composition API 是关键。同时,利用 TypeScript 的静态类型系统可以进一步提高代码质量,减少运行时错误。
空&白
- 粉丝: 145
- 资源: 1
最新资源
- 手势检测7-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- 基于python flask实现某瓣数据可视化数据分析平台
- awewq1132323
- 手写流程图检测31-YOLO(v5至v8)、COCO、CreateML、Darknet、Paligemma、TFRecord数据集合集.rar
- frida拦截微信小程序云托管API
- 肝脏及其肿瘤分割的 CT 数据集,已经切片成jpg数据,约2w张数据和mask
- 基于Java的网上教务评教管理系统的设计与实现.doc
- 2024圣诞节海外消费市场趋势及营销策略分析报告
- JWaaaaaaaaaaaaaaaaaaaa
- Python实现常见排序算法详解