File tree 5 files changed +78
-0
lines changed
questions/396-zhe-ge-tiao-zhan-kai-shi-wo-men-jiang-chang-shi-shi-xian-yi-ge-xiang-ying-shi-de-shu-ju-qing-qiu-fang-shi
5 files changed +78
-0
lines changed Original file line number Diff line number Diff line change
1
+ <script setup lang="ts">
2
+ import { ref , Ref } from ' vue'
3
+
4
+ /**
5
+ * 实现一个请求的Hooks
6
+ */
7
+ function useRequest(url : string | Ref <string >) {
8
+ const data = ref (null )
9
+ const error = ref (null )
10
+ const loading = ref (false )
11
+
12
+ /**
13
+ 你的实现
14
+ */
15
+
16
+ return {
17
+ data ,
18
+ error ,
19
+ loading
20
+ }
21
+ }
22
+
23
+ // 这里提供了一个基准的url,您可以以这个为基准创建响应式的url
24
+ // string url
25
+ const baseUrl = ' https://jsonplaceholder.typicode.com/todos/'
26
+
27
+ // Ref<string> url
28
+ /**
29
+ 你的实现
30
+ */
31
+
32
+ const { data, error, loading } = useRequest (baseUrl )
33
+
34
+ </script >
35
+
36
+ <template >
37
+ <button v-for =" i in 5" @click =" id = i" >{{ i }}</button >
38
+
39
+ <div v-if =" error" >{{ error.message }}</div >
40
+ <div v-else-if =" data" >
41
+ <pre >{{ data }}</pre >
42
+ </div >
43
+ <div v-else-if =" loading" >Loading...</div >
44
+ </template >
Original file line number Diff line number Diff line change
1
+ <!-- info-header-start-->
2
+ <!-- info-header-end-->
3
+
4
+
5
+ At the beginning of this challenge, we will try to implement a response data request method
6
+
7
+
8
+ <!-- info-footer-start-->
9
+ <!-- info-footer-end-->
Original file line number Diff line number Diff line change
1
+ <!-- info-header-start-->
2
+ <!-- info-header-end-->
3
+
4
+
5
+ 这个挑战开始,我们将尝试实现一个响应式的数据请求方式
6
+
7
+
8
+ <!-- info-footer-start-->
9
+ <!-- info-footer-end-->
Original file line number Diff line number Diff line change
1
+ difficulty : null
2
+ title : >-
3
+ At the beginning of this challenge, we will try to implement a response data
4
+ request method
5
+ tags : union, array
6
+ author :
7
+ github : sunshineLixun
8
+ name : Hello Li
9
+
Original file line number Diff line number Diff line change
1
+ difficulty : null
2
+ title : 这个挑战开始,我们将尝试实现一个响应式的数据请求方式
3
+ tags : union, array
4
+ author :
5
+ github : sunshineLixun
6
+ name : Hello Li
7
+
You can’t perform that action at this time.
0 commit comments