request.vue 2.0 KB

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