1
1
# vuex-loading
2
- 简化vuex中异步请求,不得不维护loading state的问题,支持代理异步回调与promise
2
+ Simplify vuex loading state management
3
3
4
4
## Installing
5
5
@@ -10,19 +10,19 @@ $ npm install vuex-loadings -s
10
10
```
11
11
12
12
## Example
13
-
14
-
15
13
``` js
16
14
// product.js
17
15
import vxl from ' vuex-loadings'
18
16
const api = {
17
+ // callback
19
18
listProduct (cb ,errorCb ) {
20
19
fetch (' /api/listProduct' ).then ((result ) => {
21
20
cb (result)
22
21
}).catch (error => {
23
22
errorCb (error)
24
23
})
25
24
},
25
+ // promise
26
26
productDetail (id ) {
27
27
return fetch (` /api/productDetail?id=${ id} ` )
28
28
}
@@ -38,23 +38,42 @@ const getters = {
38
38
}
39
39
}
40
40
41
- // aopLoading(commit,loadingName, fn, isPromise)
42
- // 有两种fn可以代理,第一种fn是一个promise,第二种fn是接受两个回调函数参数->成功回调和失败回调,设置isPromise可以选择指定方式代理。
43
41
const actions = {
44
42
listProduct ({{commit}}) {
43
+ // proxy callback
45
44
let request = vxl .aopLoading (commit, ' productsLoading' ,api .listProduct )
46
45
request ((result ) => {
47
46
commit (' setProducts' , result)
48
47
}, error => {
49
48
})
49
+ // it equal
50
+ // commit('setProductsLoading',true)
51
+ // api.listProduct((result) => {
52
+ // commit('setProductsLoading',false)
53
+ // commit('setProducts', result)
54
+ // }, error => {
55
+ // commit('setProductsLoading',false)
56
+ // })
50
57
},
51
58
productDetail ({{commit}}) {
59
+ // proxy promise
52
60
let request = vxl .aopLoading (commit, ' productDetail' , api .productDetail ,true )
53
61
request (1 ).then ((result ) => {
54
62
commit (' setProducts' , result)
55
63
}).catch (error => {
56
64
console .info (error)
57
65
})
66
+
67
+ // it equal
68
+ // commit('setProductDetailLoading',true)
69
+ // api.productDetail(1)
70
+ // .then(result => {
71
+ // commit('setProductDetailLoading',false)
72
+ // commit('setProducts', result)})
73
+ // .catch(error => {
74
+ // commit('setProductDetailLoading',false)
75
+ // console.info(error)
76
+ // })
58
77
}
59
78
}
60
79
@@ -67,16 +86,22 @@ const mutations = {
67
86
}
68
87
}
69
88
70
- // 使用vxl.mixin将productsLoading相关操作注入state,getters,mutations中,这样我们不需要手写大堆的代码
89
+ // it will be set loading state,loading getter,loading mutation for products,productDetail
90
+ // default loading name is productsLoading,productDetailLoading
71
91
vxl .mixin ({state,getters,mutations}, [' products' ,' productDetail' ])
72
- // 或者可以指定loading名字
92
+
93
+ // or custom loading state name
73
94
vxl .mixin ({state,getters,mutations}, [{' products' : ' productsLoading' },' productDetail' ])
74
95
75
- export {
76
- state ,getters ,actions ,mutations
96
+ export default {
97
+ namespaced: true ,
98
+ state,
99
+ getters,
100
+ actions,
101
+ mutations
77
102
}
78
103
```
79
- ### 另一种方式
104
+ ### other way
80
105
81
106
``` js
82
107
import {aopLoading , mapLoadingMutations , mapLoadingState , mapLoadingGetters } from ' ../vuex-loading'
@@ -90,25 +115,30 @@ const api = {
90
115
}
91
116
}
92
117
const state = {
93
- // 为clues绑定loading,这种方法不支持自定义loading name,默认规则是clues -> cluesLoading
94
118
... mapLoadingState ({
95
119
clues: {},
96
120
})
121
+ // it will be -> {
122
+ // clues: {}
123
+ // cluesLoading: {}
124
+ // }
97
125
}
98
126
99
127
100
128
const getters = {
101
129
clues (state ) {
102
130
return state .clues
103
131
},
132
+
133
+ // set getter function for cluesLoading
104
134
... mapLoadingGetters ([' cluesLoading' ])
105
- // 或者是自定义getters名
135
+ // or custom getter function name
106
136
... mapLoadingGetters ({' cluesLoading' : ' getCluesLoading' })
107
137
}
108
138
109
139
const actions = {
110
140
listClue ({commit}) {
111
- // 同上
141
+ // the same
112
142
let request = aopLoading (commit, ' cluesLoading' , marketApi .listClue )
113
143
request (items => {
114
144
commit (' setClues' , {items})
@@ -122,7 +152,7 @@ const mutations = {
122
152
setClues (state , {items}) {
123
153
state .clues = items
124
154
},
125
- // 绑定mutations方法
155
+ // set mutation function for cluesLoading
126
156
... mapLoadingMutations ([' cluesLoading' ])
127
157
}
128
158
0 commit comments