-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
demo.vue
79 lines (72 loc) · 1.66 KB
/
demo.vue
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
72
73
74
75
76
77
78
79
<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import { stringify } from '@vueuse/docs-utils'
import { useToggle } from '@vueuse/shared'
import { useFetch } from '@vueuse/core'
const url = ref('https://httpbin.org/get')
const refetch = ref(false)
const toggleRefetch = useToggle(refetch)
const {
data,
error,
abort,
statusCode,
isFetching,
isFinished,
canAbort,
execute,
} = useFetch(url, { refetch }).get()
const text = stringify(reactive({
isFinished,
isFetching,
canAbort,
statusCode,
error,
data: computed(() => {
try {
return JSON.parse(data.value as string)
}
catch (e) {
return null
}
}),
}))
</script>
<template>
<div>
<div>
<note>The following URLs can be used to test different features of useFetch</note>
<div class="mt-2">
Normal Request:
<code>
https://httpbin.org/get
</code>
</div>
<div>
Abort Request:
<code>
https://httpbin.org/delay/10
</code>
</div>
<div>
Response Error:
<code>
http://httpbin.org/status/500
</code>
</div>
</div>
<input v-model="url" type="text">
<button @click="execute">
Execute
</button>
<button @click="toggleRefetch">
<i v-if="refetch" inline-block align-middle i-carbon-checkmark />
<i v-else inline-block align-middle i-carbon-error />
<span class="ml-2">{{ refetch ? 'Refetch On': 'Refetch Off' }}</span>
</button>
<button v-if="canAbort" class="orange" @click="abort">
Abort
</button>
<pre class="code-block">{{ text }}</pre>
</div>
</template>