Skip to content

Commit 469d3ff

Browse files
author
浪里行舟
authored
Update README.md
1 parent b5ae1c8 commit 469d3ff

1 file changed

Lines changed: 39 additions & 13 deletions

File tree

  • vue2.0学习/基于axios接口封装

vue2.0学习/基于axios接口封装/README.md

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,42 @@ npm install
99
```
1010
npm run serve
1111
```
12-
13-
### Compiles and minifies for production
14-
```
15-
npm run build
16-
```
17-
18-
### Lints and fixes files
19-
```
20-
npm run lint
21-
```
22-
23-
### Customize configuration
24-
See [Configuration Reference](https://cli.vuejs.org/config/).
12+
## api接口的模块化管理规范
13+
单独模块管理接口,其中http.js文件就是封装好的接口请求库
14+
```
15+
// personal.js文件
16+
import axios from './http';
17+
export default {
18+
login() {
19+
return axios.post('/login');
20+
},
21+
// ...
22+
};
23+
```
24+
定义统一接口请求的入口 api.js
25+
```
26+
// api.js文件
27+
import personal from './personal';
28+
export default {
29+
personal
30+
};
31+
```
32+
最后注入到vue的原型上
33+
```
34+
// main.js
35+
import api from './api/api.js';
36+
Vue.prototype.$api=api;
37+
```
38+
这样就可以在组件中使用了
39+
```
40+
//=>在组件中使用
41+
methods: {
42+
login() {
43+
this.$api.person.login().then(result => {
44+
console.log(result) // 直接就可以调用api,无需再引入api.js文件
45+
// 业务逻辑
46+
})
47+
}
48+
}
49+
```
50+
**本代码中提供了两种封装方式:axios和fetch,分别对应http.js和request.js文件**

0 commit comments

Comments
 (0)