axios-miniprogram/docs/pages/guide/quick-start.md

127 lines
2.5 KiB
Markdown
Raw Normal View History

2023-03-25 16:22:01 +08:00
---
2023-04-15 16:21:54 +08:00
title: 开始
2023-03-25 16:22:01 +08:00
---
2023-04-15 16:21:54 +08:00
# {{ $frontmatter.title }}
2023-03-25 16:22:01 +08:00
## 安装
:::: code-group
2023-04-14 23:15:49 +08:00
```bash [NPM]
$ npm install -D axios-miniprogram
2023-03-25 16:22:01 +08:00
```
2023-04-14 23:15:49 +08:00
```bash [YARN]
$ yarn add -D axios-miniprogram
2023-03-25 16:22:01 +08:00
```
2023-04-14 23:15:49 +08:00
```bash [PNPM]
$ pnpm install -D axios-miniprogram
2023-03-25 16:22:01 +08:00
```
::::
2023-04-15 12:20:09 +08:00
[原生小程序也可以直接下载源码包](https://github.com/zjx0905/axios-miniprogram/releases)
2023-03-25 16:22:01 +08:00
2023-04-15 12:20:09 +08:00
## 引用
2023-03-25 16:22:01 +08:00
2023-04-15 12:20:09 +08:00
:::: code-group
```ts [ES Module]
2023-03-25 16:22:01 +08:00
import axios from 'axios-miniprogram';
2023-04-15 12:20:09 +08:00
axios('test');
2023-03-25 16:22:01 +08:00
```
2023-04-15 16:21:54 +08:00
```ts [CommonJS]
2023-04-15 12:20:09 +08:00
const axios = require('axios-miniprogram').default;
2023-03-25 16:22:01 +08:00
2023-04-15 12:20:09 +08:00
axios('test');
```
::::
## 使用
### `axios(url, config?)`
可以通过把 `url``config` 传递给 `axios` 来发送请求。
注意: `config` 为选填
2023-03-25 16:22:01 +08:00
2023-04-13 14:16:11 +08:00
```ts
2023-04-15 12:20:09 +08:00
import axios from 'axios-miniprogram';
// 默认发送 GET 请求
axios('https://api.com/test')
2023-03-25 16:22:01 +08:00
.then((response) => {
// 请求成功后做些什么
})
.catch((error) => {
// 请求失败后做些什么
});
// 发送 POST 请求
2023-04-15 12:20:09 +08:00
axios('https://api.com/test', {
method: 'POST',
2023-03-25 16:22:01 +08:00
})
.then((response) => {
// 请求成功后做些什么
})
.catch((error) => {
// 请求失败后做些什么
});
```
2023-04-15 12:20:09 +08:00
### `axios(config)`
2023-03-25 16:22:01 +08:00
2023-04-15 12:20:09 +08:00
也可以直接把 `config` 传递给 `axios` 来发送请求。
2023-03-25 16:22:01 +08:00
2023-04-13 14:16:11 +08:00
```ts
2023-04-15 12:20:09 +08:00
import axios from 'axios-miniprogram';
2023-03-25 16:22:01 +08:00
// 默认发送 GET 请求
2023-04-15 12:20:09 +08:00
axios({
url: 'https://api.com/test',
})
2023-03-25 16:22:01 +08:00
.then((response) => {
// 请求成功后做些什么
})
.catch((error) => {
// 请求失败后做些什么
});
// 发送 POST 请求
2023-04-15 12:20:09 +08:00
axios({
url: 'https://api.com/test',
method: 'POST',
2023-03-25 16:22:01 +08:00
})
.then((response) => {
// 请求成功后做些什么
})
.catch((error) => {
// 请求失败后做些什么
});
```
2023-04-15 16:21:54 +08:00
也可以使用请求方法简化请求。
- [axios.request(url, config?) | axios.request(config)](/basics/request)
2023-04-16 00:01:40 +08:00
- [axios.options(url, config?)](/method/OPTIONS)
- [axios.get(url, params?, config?)](/method/GET)
- [axios.head(url, params?, config?)](/method/HEAD)
- [axios.post(url, data?, config?)](/method/POST)
- [axios.put(url, data?, config?)](/method/PUT)
- [axios.patch(url, data?, config?)](/method/PATCH)
- [axios.delete(url, params?, config?)](/method/DELETE)
- [axios.trace(url, config?)](/method/TRACE)
- [axios.connect(url, config?)](/method/CONNECT)
2023-04-15 12:20:09 +08:00
还提供了一系列工具方法。
- `axios.create(defaults?)` 创建新的 `axios` 实例
- `axios.createAdapter(platform)` 创建平台适配器
- `axios.isCancel(error)` 判断异常是否来自取消请求
- `axios.isAxiosError(error)` 判断异常是否来自请求响应