vue3使用defer优化项目白屏加载时间

defer.ts

javascript 复制代码
import { ref } from 'vue'

export function useDefer() {
  const frameCount = ref(1)
  function updateFrameCount() {
    requestAnimationFrame(() => {
      frameCount.value++
      updateFrameCount()
    })
  }
  updateFrameCount()
  return function (n: any) {
    return frameCount.value >= n
  }
}

在.vue文件中使用,按帧数加载组件

javascript 复制代码
<template>
	<div>
		<Header v-if="defer(1)"></Header>
		<Main v-if="defer(2)"></Main>
		<Footer v-if="defer(3)"></Footer>
	</div>
</template>
<script>
	import { useDefer } from '@/utils/defer'
	const defer = useDefer()
</script>
相关推荐
柒和远方11 小时前
从审计日志到可下载证据包:事务型 Outbox、租约 fencing 与保留水位
前端·后端·架构
卓怡学长11 小时前
w266基于spring boot + vue 圣地延安美食乐享系统
java·数据库·vue.js·spring boot·spring·intellij-idea
minglie112 小时前
npm ali-oss库依赖冲突
前端·npm
CryptoPP12 小时前
BSE股票K线数据接入实战:从接口调用到前端图表展示
大数据·前端·网络·人工智能·websocket·网络协议
没落英雄14 小时前
5. 从零开始搭建一个 AI Agent —— 人机协作与中断恢复
前端·人工智能·架构
品尚公益团队14 小时前
C#在asp.net网页开发中的带cookie登录实现
前端·c#·asp.net
多加点辣也没关系15 小时前
JavaScript|第9章:对象 — 基础
开发语言·javascript·ecmascript
没有了遇见15 小时前
AI Agent 是什么?—— 一文理解 LLM、Memory、Skills、Tools、MCP、Workflow,Context
android·前端·程序员
用户831348593069815 小时前
Cesium自定义可调节星空夜景+星星闪烁
vue.js·cesium
后台开发者Ethan16 小时前
setState组件更新
前端·javascript·react