【vue+ts】性能优化,defer优化白屏

代码:

import {ref} from 'vue'

export function useDefer(maxCount = 100) {
    const count = ref(0)
    let raqId:any = null;
    function updateFrame() {
        count.value++
        if (count.value >= maxCount) {
            return;
        }
        raqId = requestAnimationFrame(updateFrame)
    }
    updateFrame();
    onUnmounted(() => {
        cancelAnimationFrame(raqId)
    })
    return function (n:number) {
        return count.value >= n
    }
}

用法:

<template>
    <IndexBbsRotationcArea :homeData="homeData" v-if="defer(1)" />
    <IndexBbsRecommendArea v-if="defer(2)" />
    <IndexBbsRecommendArea v-if="defer(3)" />
    <IndexBbsRecommendArea v-if="defer(4)" />
    <IndexBbsRecommendArea v-if="defer(5)" />
</template>

<script setup lang="ts">
    import { useDefer } from "~/utils/defer";
    const defer = useDefer();
</script>

上一篇:Wakeup Source框架设计与实现


下一篇:正则表达式基础