defineLazyHydrationComponent

源文件
使用特定策略定义延迟注水组件。

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 的行为。

阅读有关 hydrate-on-visible 选项的更多信息。
底层,这使用了 Vue 的内置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 属性是可选的。您可以传递一个正数来指定最大超时时间。

空闲策略适用于当浏览器空闲时可以注水的组件。

底层,这使用了 Vue 的内置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 属性是可选的。如果您不传递事件或事件列表,它将默认在 pointerenterclickfocus 上注水。

底层,这使用了 Vue 的内置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>
底层,这使用了 Vue 的内置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