1.定义
axios是一款sjsx请求工具
特点:
01 前后端都可以使用
02 不依赖dom
03 拦截扩展强调
04 可封装复用性强
2.安装
cd 项目目录
npm i axios -S
3.在vue全局挂载
01 导入main.js
import axios from 'axios'
02挂载
Vue.prototype.$axios=axios
03使用
this.$axios.xxx
4.便捷方法
.post(url,data,config)
.get(url,config) ---get传递参数给后端
?参数名=参数值&参数名2=参数值2?current=2
.delete(url,config)
删除
.put(url,data,config)
修改
5.基础方法
axios({
url:""请求地址
method:""请求方法get post put delete
data post请求的数据
params:,get请求的数据
headers:,请求头配置
})
6.执行结果
网络请求成功
.then(res=>{
res.data请求返回的数据
})
网络请求失败
.catch(err=>{
err.response.data返回失败数据
})
7.config axios配置
headers:
请求头 ------添加token
"Authorization":'Bearer '+localStorage.getItem("token")
8.restFul
1 接口设计风格
2 强调每个url地址都是一个资源
3 可以通过get post put delete操作资源
4 get获取 post新增 put修改 delete删除