@@ -23,7 +23,7 @@ this.$axios.$post('/security/users', body)
23
23
## 使用
24
24
25
25
``` javascript
26
- // 创建一个 API 资源,修改文件 src/api/index .js
26
+ // 创建一个 API 资源,修改文件 src/services/basis .js
27
27
28
28
// 创建了一个菜单资源的 CRUD 接口方法
29
29
+ export const menus = new Repository (` ${ DEEPEXI_CLOUD_TENANT } /${ VERSION } /menus` )
@@ -33,23 +33,23 @@ this.$axios.$post('/security/users', body)
33
33
// 在 page 中
34
34
mounted () {
35
35
// 获取资源的服务器路径
36
- this .$http .menus .uri ()
36
+ this .$services . basis .menus .uri ()
37
37
// 获取所有菜单资源,返回一个列表
38
- this .$http .menus .list ()
38
+ this .$services . basis .menus .list ()
39
39
// 获取某个菜单资源的详情
40
- this .$http .menus .detail (MENUS_ID )
40
+ this .$services . basis .menus .detail (MENUS_ID )
41
41
// 创建一个菜单资源
42
- this .$http .menus .create (payload)
42
+ this .$services . basis .menus .create (payload)
43
43
// 更新一个菜单资源
44
- this .$http .menus .update (MENUS_ID , payload)
44
+ this .$services . basis .menus .update (MENUS_ID , payload)
45
45
// 删除一个菜单资源
46
- this .$http .menus .delete (MENUS_ID )
46
+ this .$services . basis .menus .delete (MENUS_ID )
47
47
}
48
48
49
49
// 在 store 中
50
50
export const actions = {
51
51
async getMenus (store , payload ) {
52
- const data = await this .$http .menus .detail (payload)
52
+ const data = await this .$services . basis .menus .detail (payload)
53
53
...
54
54
}
55
55
}
@@ -60,7 +60,7 @@ export const actions = {
60
60
有些时候,后端的接口并不是严格遵循 RESTful 的最佳实践,这个时候就需要自己重新实现默认的方法
61
61
62
62
``` javascript
63
- // 在 src/api /repository.js 中增加一个类,继承 Repository
63
+ // 在 src/services /repository.js 中增加一个类,继承 Repository
64
64
export class ExampleRepository extends Repository {
65
65
constructor (resource , id ) {
66
66
super (resource)
@@ -78,13 +78,58 @@ export class ExampleRepository extends Repository {
78
78
}
79
79
80
80
// 基于 ExampleRepository 创建一个 API
81
- export const example = new ExampleRepository (' /example/api' )
81
+ export default new ExampleRepository (' /example/api' )
82
82
83
83
// 调用
84
- this .$http .example .uri (appId)
85
- this .$http .example .detail (id)
86
- this .$http .example .list ()
87
- this .$http .example .create (payload)
88
- this .$http .example .update (appId, payload)
89
- this .$http .example .delete (id)
84
+ this .$services .example .uri (appId)
85
+ this .$services .example .detail (id)
86
+ this .$services .example .list ()
87
+ this .$services .example .create (payload)
88
+ this .$services .example .update (appId, payload)
89
+ this .$services .example .delete (id)
90
90
```
91
+ ## 注意
92
+
93
+ services 会根据文件名作为 scope,将该文件 export 出去的所有值挂在这个 scope 下。
94
+
95
+ 比如 ` basic.js `
96
+ ``` js
97
+ export const login = new Repository (` ${ SECURITY_CLOUD } /${ VERSION } /login` )
98
+
99
+ export const userInfo = new Repository (` ${ SECURITY_CLOUD } /${ VERSION } /token` )
100
+
101
+ export const menus = new Repository (` ${ SECURITY_CLOUD_TENANT } /${ VERSION } /menus` )
102
+
103
+ export const subMenus = new Repository (
104
+ ` ${ SECURITY_CLOUD_TENANT } /${ VERSION } /sub-menus` ,
105
+ )
106
+ ```
107
+
108
+ 就会将它们挂在 ` this.$services.basic ` 下:
109
+ ``` js
110
+ this .$services .basic .menus .list ()
111
+ ```
112
+
113
+ 如果文件中有 ` export default ` ,则会有些不同。
114
+
115
+ 比如 ` example.js ` :
116
+ ``` js
117
+ export default new Repository (` ${ VERSION } /example/api` )
118
+
119
+ // 使用
120
+ this .$services .example .list ()
121
+ ```
122
+
123
+ 如果文件中既有 ` export default ` 又有 ` export const ` :
124
+ ``` js
125
+ export default new Repository (` ${ VERSION } /example/api` )
126
+
127
+ export const other = new Repository (` ${ VERSION } /example/api/other` )
128
+
129
+ // 使用
130
+ this .$services .example .list ()
131
+ this .$services .example .other .list ()
132
+ ```
133
+
134
+ ** 需要注意** :导出的 ` service ` 名字不能与 ` Repository ` 的接口名字重复,接口名字列表可以在这里看到:
135
+ https://github.com/femessage/create-nuxt-app/blob/dev/template/modules/service/services/common/repository.js#L8
0 commit comments