request.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <route lang="json5">
  2. {
  3. layout: 'demo',
  4. style: {
  5. navigationBarTitleText: '请求',
  6. },
  7. }
  8. </route>
  9. <script lang="ts" setup>
  10. import type { IFooItem } from '@/service/index/foo'
  11. import { getFooAPI } from '@/service/index/foo'
  12. // import { findPetsByStatusQueryOptions } from '@/service/app'
  13. // import { useQuery } from '@tanstack/vue-query'
  14. const recommendUrl = ref('http://laf.run/signup?code=ohaOgIX')
  15. // const initialData = {
  16. // name: 'initialData',
  17. // id: '1234',
  18. // }
  19. const initialData = undefined
  20. // 适合少部分全局性的接口————多个页面都需要的请求接口,额外编写一个 Service 层
  21. const { loading, error, data, run } = useRequest<IFooItem>(() => getFooAPI('菲鸽'), {
  22. immediate: true,
  23. initialData,
  24. })
  25. // 使用 vue-query 的 useQuery 来请求数据,只做参考,是否使用请根据实际情况而定
  26. // const {
  27. // data: data2,
  28. // error: error2,
  29. // isLoading: isLoading2,
  30. // refetch,
  31. // } = useQuery(findPetsByStatusQueryOptions({ params: { status: ['available'] } }))
  32. function reset() {
  33. data.value = initialData
  34. }
  35. </script>
  36. <template>
  37. <view class="p-6 text-center">
  38. <view class="my-2">
  39. 使用的是 laf 云后台
  40. </view>
  41. <view class="text-green-400">
  42. 我的推荐码,可以获得佣金
  43. </view>
  44. <!-- #ifdef H5 -->
  45. <view class="my-2">
  46. <a class="my-2" :href="recommendUrl" target="_blank">{{ recommendUrl }}</a>
  47. </view>
  48. <!-- #endif -->
  49. <!-- #ifndef H5 -->
  50. <view class="my-2 text-left text-sm">
  51. {{ recommendUrl }}
  52. </view>
  53. <!-- #endif -->
  54. <!-- http://localhost:9000/#/pages/index/request -->
  55. <wd-button class="my-6" @click="run">
  56. 发送请求
  57. </wd-button>
  58. <view class="h-16">
  59. <view v-if="loading">
  60. loading...
  61. </view>
  62. <block v-else>
  63. <view class="text-xl">
  64. 请求数据如下
  65. </view>
  66. <view class="text-green leading-8">
  67. {{ JSON.stringify(data) }}
  68. </view>
  69. </block>
  70. </view>
  71. <wd-button type="error" class="my-6" :disabled="!data" @click="reset">
  72. 重置数据
  73. </wd-button>
  74. </view>
  75. </template>