点此查看 所有教程、视频、源码导航
文章目录
-
- [1. 背景信息](#1. 背景信息)
- [2. 安装 Axios](#2. 安装 Axios)
- [3. 简单引入并发送 get 请求](#3. 简单引入并发送 get 请求)
- [4. 发送 post 请求,传递 json 数据](#4. 发送 post 请求,传递 json 数据)
- [5. 配置基础地址 baseURL](#5. 配置基础地址 baseURL)
- [6. 请求头 headers 基础配置](#6. 请求头 headers 基础配置)
- [7. 捕获请求异常](#7. 捕获请求异常)
- [8. 小结](#8. 小结)
1. 背景信息
前面我们学习了 Pinia 管理前端状态,实际项目中,前端的数据大多来自后端接口。
Axios 是 Vue 项目最常用的 HTTP 请求库,用来发送网络请求,获取、提交数据。
本节课学习 Axios 基础引入、get、post 请求,以及基础参数传递方式。
2. 安装 Axios
使用 npm 命令安装 axios。
shell
npm install axios
注意:
- axios 不是 Vue 内置包,必须手动安装。
- 版本不同,基础用法基本一致,不影响本节基础学习。
3. 简单引入并发送 get 请求
在组件中直接导入 axios,发送 get 请求,参数拼接在 url 上。
html
<template>
<div>
<button @click="getData">发送get请求</button>
</div>
</template>
<script setup>
import axios from 'axios'
const getData = async () => {
// get 参数直接写在url后面,也可以使用params配置
const res = await axios.get('https://httpbin.org/get', {
params: {
name: '张三',
age: 18
}
})
console.log(res.data)
}
</script>
效果:

注意:
params配置项,会自动把对象拼接到 url 的查询字符串。- get 请求适合获取数据,不适合传递大量或者敏感数据。
- axios 返回的是 Promise,可以搭配 async / await 同步写法。
4. 发送 post 请求,传递 json 数据
post 请求一般用来提交新增、修改的数据,默认以 json 格式传递给后端。
html
<template>
<div>
<button @click="postData">发送post请求</button>
</div>
</template>
<script setup>
import axios from 'axios'
const postData = async () => {
// 第二个参数是请求体,json格式传给后端
const res = await axios.post('https://httpbin.org/post', {
username: 'admin',
password: '123456'
})
console.log(res.data)
}
</script>
效果:

注意:
- post 的第二个参数是请求体,不需要写 params。
- 后端接口需要接收 json 格式,才能正常拿到数据。
- post 相比 get,参数不会直接暴露在地址栏。
5. 配置基础地址 baseURL
多次请求时,重复写长域名很麻烦,可以配置 baseURL,统一基础地址。
html
<template>
<div>
<button @click="testBase">测试baseURL</button>
</div>
</template>
<script setup>
import axios from 'axios'
// 创建axios实例,统一配置基础地址
const request = axios.create({
baseURL: 'https://httpbin.org'
})
const testBase = async () => {
// 只写后面接口路径,自动拼接baseURL
const res = await request.get('/get', {
params: {
msg: '测试base'
}
})
console.log(res.data)
}
</script>
注意:
axios.create创建独立实例,推荐项目中使用实例,方便统一管理。- baseURL 末尾不要加多余斜杠,路径开头按需写斜杠。
- 不同业务接口地址不一样,可以创建多个 axios 实例。
6. 请求头 headers 基础配置
部分接口需要携带 token、指定内容类型,通过 headers 配置。
html
<template>
<div>
<button @click="testHeader">测试请求头</button>
</div>
</template>
<script setup>
import axios from 'axios'
const testHeader = async () => {
const res = await axios.get('https://httpbin.org/get', {
params: {
id: 100
},
headers: {
token: 'abc123456'
}
})
console.log(res.data)
}
</script>
注意:
- headers 放在请求配置对象里面,和 params 同级。
- 登录凭证、权限标识一般放在请求头传递。
- 不要在请求头随意放大量业务数据。
7. 捕获请求异常
网络错误、接口 404、500 都会抛出异常,使用 try-catch 捕获。
html
<template>
<div>
<button @click="safeRequest">带异常捕获的请求</button>
</div>
</template>
<script setup>
import axios from 'axios'
const safeRequest = async () => {
try {
const res = await axios.get('https://httpbin.org/get')
console.log('请求成功:', res.data)
} catch (err) {
console.log('请求失败:', err)
}
}
</script>
注意:
- 线上项目必须捕获异常,避免接口报错直接导致页面逻辑卡死。
- err.response 可以拿到后端返回的错误状态码和错误信息。
- 网络断开跨域这类情况,err.response 不存在,需要单独判断。
8. 小结
- Axios 是 Vue 项目常用网络库,安装之后 import 引入使用。
- get 请求多用于查询数据,参数使用 params;post 多用于提交数据,参数放在请求体。
axios.create可以创建实例,统一配置 baseURL、headers。- 使用 async / await 配合 try-catch 处理请求成功和失败。
- get 参数暴露在地址栏,敏感数据尽量不要使用 get 传递。