axios是一款ajax请求工具,它是基于Promis的。
axios有如下特点:
- 前后端都可以使用
- 不依赖dom
- 拦截扩展强调
- 可封装复用性强
- 首先进入项目目录: cd 项目目录
- 安装axios: npm i axios -S
1.导入 main.js
import axios from 'axios'
2.挂载
Vue.prototype.$axios = axios;
3.使用
this.$axios.xxx
axios有便捷方法和基础方法两种方法:
- post(url,data,config)
- get(url,config)
- .delete(url,config) 删除
- .put(url,data,config) 修改
axios({
url,//请求的地址
method,// 请求方法 get,post,put,delete
data,//post请求的数据
params,//get请求的数据
headers,//请求头配置
})
网络请求成功
.then(res=>{
res.data 请求返回的数据
})请求失败
.catch(err=>{
err.response.data 返回失败数据
})
- headers: {
- Authorization: "Bearer " + localStorage.getItem("token"),
- },