request.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <script lang="ts" setup>
  2. import type { IFooItem } from '@/api/foo'
  3. import { getFooAPI } from '@/api/foo'
  4. const recommendUrl = ref('http://laf.run/signup?code=ohaOgIX')
  5. // const initialData = {
  6. // name: 'initialData',
  7. // id: '1234',
  8. // }
  9. const initialData = undefined
  10. const { loading, error, data, run } = useRequest<IFooItem>(() => getFooAPI('菲鸽'), {
  11. immediate: true,
  12. initialData,
  13. })
  14. function reset() {
  15. data.value = initialData
  16. }
  17. </script>
  18. <template>
  19. <view class="p-6 text-center">
  20. <view class="my-2">
  21. pages 里面的 vue 文件会扫描成页面,将自动添加到 pages.json 里面。
  22. </view>
  23. <view class="my-2 text-green-400">
  24. 但是 pages/components 里面的 vue 不会。
  25. </view>
  26. <view class="my-4 text-center">
  27. <button type="primary" size="mini" class="w-160px" @click="run">
  28. 发送请求
  29. </button>
  30. </view>
  31. <view class="h-16">
  32. <view v-if="loading">
  33. loading...
  34. </view>
  35. <block v-else>
  36. <view class="text-xl">
  37. 请求数据如下
  38. </view>
  39. <view class="text-green leading-8">
  40. {{ JSON.stringify(data) }}
  41. </view>
  42. </block>
  43. </view>
  44. <view class="my-4 text-center">
  45. <button type="warn" size="mini" class="w-160px" :disabled="!data" @click="reset">
  46. 重置数据
  47. </button>
  48. </view>
  49. </view>
  50. </template>