-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (39 loc) · 862 Bytes
/
index.js
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
import { ref } from "vue"
/**
* 空函数
*/
const noop = () => { }
/**
* 空 payload 对象
*/
const emptyPayload = Object.create(null)
/**
* 转换函数
* @param {Function} api uniapp 的 api
* @returns useApi
*/
export const $U = (api = noop) => {
const useApi = (payload = emptyPayload) => {
const result = ref(null)
const loading = ref(true) // 加载状态,开始时为 true
const error = ref(null)
api({
...payload,
success(r) {
result.value = r
},
fail(e) {
error.value = e
},
complete() {
loading.value = false // 加载状态,结束时为 false
}
})
return {
result,
loading,
error
}
}
return useApi
}