PostView.vue 876 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <script setup lang="ts">
  2. import type { PostSketch } from '@/models';
  3. import { onMounted, ref } from 'vue';
  4. import { api } from '@/utils/axios';
  5. import PostCard from '@/components/PostCard.vue';
  6. const data = ref<PostSketch[]>([]);
  7. const pageSize = 10;
  8. const isEnd = ref(false);
  9. function nextDatum() {
  10. if (isEnd.value) {
  11. return;
  12. }
  13. api.postList(pageSize, data.value.length).then((res) => {
  14. if (res.data.length < pageSize) {
  15. isEnd.value = true;
  16. }
  17. data.value = [...data.value, ...res.data];
  18. }).catch((err) => {
  19. console.error(err);
  20. });
  21. }
  22. onMounted(() => {
  23. nextDatum();
  24. });
  25. </script>
  26. <template>
  27. <div>
  28. <PostCard v-for="item of data" :key="item.id" :target="item" />
  29. <div v-if="isEnd">
  30. 没有更多了
  31. </div>
  32. <div v-else @click="nextDatum">
  33. 显示更多
  34. </div>
  35. </div>
  36. </template>
  37. <style scoped>
  38. </style>