Reka UI 徽标Reka
backdrop
实用工具

存在(Presence)

管理元素的挂载和卸载,并支持过渡效果。
问题

此组件与 Vue Transition 有何不同?

答:最大的区别在于它接受 CSS 动画,并控制元素的可见性。

Presence 组件提供了对元素挂载/卸载的增强控制。它确保动画和过渡在元素从 DOM 中移除之前完成,使其成为动画 UI 组件的理想选择。

API 参考

属性默认类型
present*
布尔值

条件性地挂载或卸载子元素。类似于 v-if

forceMount
布尔值

强制元素始终渲染。这对于通过暴露的 present 编程方式渲染孙子组件非常有用。

事件触发载荷
enter
CustomEvent

进入动画开始时调用的事件处理程序

after-enter
CustomEvent

进入动画完成时调用的事件处理程序

leave
CustomEvent

离开动画开始时调用的事件处理程序

after-leave
CustomEvent

离开动画完成时调用的事件处理程序

提示

阅读我们的 动画指南,了解更多关于使用 Presence 组件实现动画的信息。

示例

vue
<template>
  <Presence :present="isVisible">
    <div
      :data-open="isVisible ? 'open' : 'close'"
      class="data-[state=open]:animate-fadeIn data-[state=closed]:animate-fadeOut"
    >
      <slot />
    </div>
  </Presence>
</template>

强制挂载

当您需要确保内容始终被渲染,无论其存在状态如何时

vue
<template>
  <Presence v-slot="{ present }" :present="isVisible" :force-mount="true">
    <div>
      This content will always be rendered

      <div v-if="present">
        This content is hidden
      </div>
    </div>
  </Presence>
</template>