Skip to content

Commit

Permalink
数据懒加载
Browse files Browse the repository at this point in the history
  • Loading branch information
upangka committed Dec 8, 2022
1 parent 266a844 commit 4d52c15
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
10 changes: 3 additions & 7 deletions src/components/home/HomeHot.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<PanelComponent main-title="人气推荐" sub-title="人气爆款 不容错过">
<div class="transition-wrapper">
<div class="transition-wrapper" ref="target">
<Transition name="fade">
<template v-if="goods.length === 0">
<HomeSkeleton bg-color="#f0f9f4"></HomeSkeleton>
Expand All @@ -19,16 +19,12 @@
</PanelComponent>
</template>
<script lang="ts" setup>
import { onMounted, ref, type Ref } from "vue"
import PanelComponent from "@/components/libraries/panel/PanelComponent.vue"
import HomeSkeleton from "@/components/home/HomeSkeleton.vue"
import { findHot } from "@/api/home"
import type { GoodsHot as Goods } from "@/types/home/home"
import { useLazyData } from "@/composables/useLazyData"
const goods: Ref<Goods[]> = ref([])
onMounted(async () => {
goods.value = await findHot()
})
const { result: goods, target } = useLazyData(findHot)
</script>

<style scoped lang="scss">
Expand Down
6 changes: 5 additions & 1 deletion src/composables/useLazyData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { ref, onMounted, type Ref } from "vue"
import { useIntersectionObserver } from "./useIntersectionObserver"

type GetData<T> = () => Promise<T[]>

/**
* 数据懒加载
* @param apiFn 请求的API
* @returns
*/
export function useLazyData<T>(apiFn: GetData<T>) {
const target = ref(null)
const result: Ref<T[]> = ref([])
Expand Down

0 comments on commit 4d52c15

Please sign in to comment.