记录项目中axios的封装及api调用方法
入口
@/index.js:
1 | import Vue from 'vue'; |
axios封装
@/plugins/axios.js:
1 | import axios from 'axios'; |
拦截器
@/config/interceptor/axios.js:
1 | import axios from 'axios'; |
@/utils/auth/index.js:
1 | import Cookies from 'js-cookie'; |
@/config/index.js:
1 | // axios 默认配置 |
loadingView请求中状态
@/plugins/loadingView.js:
1 | /** |
样式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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71.loadingView {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
// margin-left: -50px;
// margin-right: -50px;
// margin-top: -50px;
z-index: 1999;
display: none;
background-color: rgba(255, 255, 255, 0.1);
// background-color: red;
.loadingViewFrame {
position: absolute;
left: 50%;
top: 50%;
margin: -20px 0 0 -20px;
width: 40px;
height: 40px;
-webkit-transform: rotateZ(45deg);
transform: rotateZ(45deg);
.loadingBox {
float: left;
width: 50%;
height: 50%;
position: relative;
-webkit-transform: scale(1.1);
-ms-transform: scale(1.1);
transform: scale(1.1);
}
.loadingBox:before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: @baseThemeColor;
-webkit-animation: foldCubeAngle 2.4s infinite linear both;
animation: foldCubeAngle 2.4s infinite linear both;
-webkit-transform-origin: 100% 100%;
-ms-transform-origin: 100% 100%;
transform-origin: 100% 100%;
}
.loadingBox2 {
-webkit-transform: scale(1.1) rotateZ(90deg);
transform: scale(1.1) rotateZ(90deg);
}
.loadingBox3 {
-webkit-transform: scale(1.1) rotateZ(180deg);
transform: scale(1.1) rotateZ(180deg);
}
.loadingBox4 {
-webkit-transform: scale(1.1) rotateZ(270deg);
transform: scale(1.1) rotateZ(270deg);
}
.loadingBox2:before {
-webkit-animation-delay: 0.3s;
animation-delay: 0.3s;
}
.loadingBox3:before {
-webkit-animation-delay: 0.6s;
animation-delay: 0.6s;
}
.loadingBox4:before {
-webkit-animation-delay: 0.9s;
animation-delay: 0.9s;
}
}
}
@/plugins/index.js:
1 | import loadingView from './loadingView'; |
api请求方法封装
@/plugins/api.js:
1 | /** @format */ |
工具函数@/utils/index.js:
1 | // 断言的内容 |
配置@/config/index.js:
1 | // axios 默认配置 |
api接口地址
@/api/index.js:
1 | import common from './common'; // 登录 |
各个模块@/api/school-count/index.js:
1 | import schoolContact from './school-contact'; |
@/api/school-count/school-contact.js:
1 | export default [ |
使用方法
1 | this.$api['schoolManage/schoolPageList']({ |
当需设置额外的Content-Type
时:
1 | this.$api[this.uploadUrl](formData, { |
注:
- 此封装方法只能去到成功时的data值,失败时会展示接口message信息。
- 此封装方法默认
Content-Type
为application/json
。如果是如果是application/x-www-form-urlencoded
需要引入qs
模块做参数处理
当需要在js文件中使用时:
1 | import api from '@/plugins/api'; |