axios封装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
| import axios from "axios";
// 使用create创建axios实例
const Service=axios.create({
timeout:8000,
baseURL:"http:xxxxx/",
headers:{
"Content-type":"application/json;charset=utf-8",
"Authorization" : 登陆后拿到的token
}
})
// 请求拦截-增加loading,对请求做统一处理
Service.interceptors.request.use(config=>{
loadingObj=ElLoading.service({
lock: true,
text: 'Loading',
background: 'rgba(0, 0, 0, 0.7)',
})
return config
})
// 响应拦截-对返回值做统一处理
Service.interceptors.response.use(response=>{
const data=response.data
return data
},error=>{
})
// post请求
export const post=config=>{
return Service({
...config,
method:"post",
data:config.data
})
}
// get请求
export const get=config=>{
return Service({
...config,
method:"get",
params:config.data
})
}
|
接口封装
import {get,post} from "./service"
export const loginApi=data=>{
return post({
url:'/login',
data
})
}
使用
import {loginApi} from "@/util/request"
loginApi(data).then(res=>{})