defineLazyHydrationComponent 是一个编译器宏,可帮助您创建具有特定懒惰水合策略的组件。懒惰水合将水合延迟到组件可见或浏览器完成更关键的任务。这可以显著降低初始性能成本,特别是对于非必要的组件。
当组件在视口中变得可见时水合。
<script setup lang="ts">
const LazyHydrationMyComponent = defineLazyHydrationComponent(
'visible',
() => import('./components/MyComponent.vue'),
)
</script>
<template>
<div>
<!--
Hydration will be triggered when
the element(s) is 100px away from entering the viewport.
-->
<LazyHydrationMyComponent :hydrate-on-visible="{ rootMargin: '100px' }" />
</div>
</template>
hydrateOnVisible 属性是可选的。您可以传入一个对象来自定义底层 IntersectionObserver 的行为。
hydrateOnVisible 策略.当浏览器空闲时水合组件。如果需要组件尽快加载但又不阻塞关键渲染路径,这很适用。
<script setup lang="ts">
const LazyHydrationMyComponent = defineLazyHydrationComponent(
'idle',
() => import('./components/MyComponent.vue'),
)
</script>
<template>
<div>
<!-- Hydration will be triggered when the browser is idle or after 2000ms. -->
<LazyHydrationMyComponent :hydrate-on-idle="2000" />
</div>
</template>
hydrateOnIdle 属性是可选的。您可以传入一个正数来指定最大超时时间。
空闲策略适用于当浏览器空闲时可以水合的组件。
hydrateOnIdle 策略.在指定的交互(例如,点击、鼠标悬停)之后水合组件。
<script setup lang="ts">
const LazyHydrationMyComponent = defineLazyHydrationComponent(
'interaction',
() => import('./components/MyComponent.vue'),
)
</script>
<template>
<div>
<!--
Hydration will be triggered when
the element(s) is hovered over by the pointer.
-->
<LazyHydrationMyComponent hydrate-on-interaction="mouseover" />
</div>
</template>
hydrateOnInteraction 属性是可选的。如果您不传入事件或事件列表,它将默认为在 pointerenter、click 和 focus 上水合。
hydrateOnInteraction 策略.当窗口匹配媒体查询时水合组件。
<script setup lang="ts">
const LazyHydrationMyComponent = defineLazyHydrationComponent(
'mediaQuery',
() => import('./components/MyComponent.vue'),
)
</script>
<template>
<div>
<!--
Hydration will be triggered when
the window width is greater than or equal to 768px.
-->
<LazyHydrationMyComponent hydrate-on-media-query="(min-width: 768px)" />
</div>
</template>
hydrateOnMediaQuery 策略.在指定延迟(毫秒)后水合组件。
<script setup lang="ts">
const LazyHydrationMyComponent = defineLazyHydrationComponent(
'time',
() => import('./components/MyComponent.vue'),
)
</script>
<template>
<div>
<!-- Hydration is triggered after 1000ms. -->
<LazyHydrationMyComponent :hydrate-after="1000" />
</div>
</template>
时间策略适用于可以等待特定时间的组件。
根据布尔条件水合组件。
<script setup lang="ts">
const LazyHydrationMyComponent = defineLazyHydrationComponent(
'if',
() => import('./components/MyComponent.vue'),
)
const isReady = ref(false)
function myFunction () {
// Trigger custom hydration strategy...
isReady.value = true
}
</script>
<template>
<div>
<!-- Hydration is triggered when isReady becomes true. -->
<LazyHydrationMyComponent :hydrate-when="isReady" />
</div>
</template>
条件策略最适用于可能并非总是需要水合的组件。
永不水合组件。
<script setup lang="ts">
const LazyHydrationMyComponent = defineLazyHydrationComponent(
'never',
() => import('./components/MyComponent.vue'),
)
</script>
<template>
<div>
<!-- This component will never be hydrated by Vue. -->
<LazyHydrationMyComponent />
</div>
</template>
所有延迟水合组件在水合时都会发出 @hydrated 事件。
<script setup lang="ts">
const LazyHydrationMyComponent = defineLazyHydrationComponent(
'visible',
() => import('./components/MyComponent.vue'),
)
function onHydrate () {
console.log('Component has been hydrated!')
}
</script>
<template>
<div>
<LazyHydrationMyComponent
:hydrate-on-visible="{ rootMargin: '100px' }"
@hydrated="onHydrated"
/>
</div>
</template>
<script setup lang="ts">
const strategy = 'visible'
const source = () => import('./components/MyComponent.vue')
const LazyHydrationMyComponent = defineLazyHydrationComponent(strategy, source)
</script>
策略'visible' | 'idle' | 'interaction' | 'mediaQuery' | 'if' | 'time' | 'never'true| 策略 | 描述 |
|---|---|
可见 | 当组件在视口中变得可见时水合。 |
空闲 | 当浏览器空闲时或延迟后水合。 |
交互 | 在用户交互(例如,点击、悬停)时水合。 |
媒体查询 | 当满足指定的媒体查询条件时水合。 |
条件 | 当满足指定的布尔条件时水合。 |
时间 | 在指定的时间延迟后水合。 |
永不 | 阻止 Vue 水合组件。 |
来源() => Promise<Component>true