vue组件有:1、component,用于渲染一个“元组件”为动态组件。2、transition,用于为单个元素或组件提供动画过渡效果。3、transition-group,用于为列表中的多个元素或组件提供过渡效果。4、keep-alive,用于缓存包裹在其中的动态切换组件。5、slot。6、teleport,用于将其插槽内容渲染到DOM中的另一个位置。7、Suspense。
本教程操作环境:windows7系统、vue3版,DELL G3电脑。
内置组件无需注册便可以直接在模板中使用。它们也是 tree-shakeable 的:仅在使用时才会包含在构建中。
在渲染函数中使用它们时,需要显式导入。例如:
import { h, Transition } from 'vue' h(Transition, { /* props */ })
Props:
立即学习“前端免费学习笔记(深入)”;
is - string | Component
渲染一个“元组件”为动态组件。依 is 的值,来决定哪个组件被渲染。is 的值是一个字符串,它既可以是 HTML 标签名称也可以是组件名称。
<!-- 动态组件由 vm 实例的 `componentId` property 控制 --> <component :is="componentId"></component> <!-- 也能够渲染注册过的组件或 prop 传入的组件--> <component :is="$options.components.child"></component> <!-- 可以通过字符串引用组件 --> <component :is="condition ? 'FooComponent' : 'BarComponent'"></component> <!-- 可以用来渲染原生 HTML 元素 --> <component :is="href ? 'a' : 'span'"></component>
Props:
立即学习“前端免费学习笔记(深入)”;
name - string 用于自动生成 CSS 过渡类名。例如:name: 'fade' 将自动拓展为 .fade-enter,.fade-enter-active 等。
appear - boolean,是否在初始渲染时使用过渡。默认为 false。
css - boolean。是否使用 CSS 过渡类。默认为 true。如果设置为 false,将只通过组件事件触发注册的 JavaScript 钩子。
type - string。指定过渡事件类型,侦听过渡何时结束。有效值为 "transition" 和 "animation"。默认 Vue.js 将自动检测出持续时间长的为过渡事件类型。mode - string 控制离开/进入过渡的时间序列。有效的模式有 "out-in" 和 "in-out";默认同时进行。
duration - number | { enter : number, leave : number }。指定过渡的持续时间。默认情况下,Vue 会等待过渡所在根元素的第一个 transitionend 或 animationend 事件。enter-from-class - string
leave-from-class - stringappear-class - string
enter-to-class - stringleave-to-class - string
appear-to-class - stringenter-active-class - string
leave-active-class - stringappear-active-class - string
事件:
before-enter
before-leave
leave
appearafter-enter
after-leave
after-appear
enter-cancelled
leave-cancelled (仅 v-show)appear-cancelled
<!-- 动态组件由 vm 实例的 `componentId` property 控制 --> <component :is="componentId"></component> <!-- 也能够渲染注册过的组件或 prop 传入的组件--> <component :is="$options.components.child"></component> <!-- 可以通过字符串引用组件 --> <component :is="condition ? 'FooComponent' : 'BarComponent'"></component> <!-- 可以用来渲染原生 HTML 元素 --> <component :is="href ? 'a' : 'span'"></component>
const app = Vue.createApp({ ... methods: { transitionComplete (el) { // 因为传递了'el'的DOM元素作为参数 } } ... }) app.mount('#transition-demo')
tag - string,默认为 span。
move-class - 覆盖移动过渡期间应用的 CSS 类。除了 mode,其他 attribute 和
事件:
事件和注意,每个
<transition-group tag="ul" name="slide"> <li v-for="item in items" :key="item.id"> {{ item.text }} </li> </transition-group>
Props:
立即学习“前端免费学习笔记(深入)”;
include - string | RegExp | Array。只有名称匹配的组件会被缓存。
exclude - string | RegExp | Array。任何名称匹配的组件都不会被缓存。max - number | string。最多可以缓存多少组件实例。
当组件在
主要用于保留组件状态或避免重新渲染。
<!-- 基本 --> <keep-alive> <component :is="view"></component> </keep-alive> <!-- 多个条件判断的子组件 --> <keep-alive> <comp-a v-if="a > 1"></comp-a> <comp-b v-else></comp-b> </keep-alive> <!-- 和 `<transition>` 一起使用 --> <transition> <keep-alive> <component :is="view"></component> </keep-alive> </transition>
注意,
The include 和 exclude prop 允许组件有条件地缓存。二者都可以用逗号分隔字符串、正则表达式或一个数组来表示:
<!-- 逗号分隔字符串 --> <keep-alive include="a,b"> <component :is="view"></component> </keep-alive> <!-- regex (使用 `v-bind`) --> <keep-alive :include="/a|b/"> <component :is="view"></component> </keep-alive> <!-- Array (使用 `v-bind`) --> <keep-alive :include="['a', 'b']"> <component :is="view"></component> </keep-alive>
匹配首先检查组件自身的 name 选项,如果 name 选项不可用,则匹配它的局部注册名称 (父组件 components 选项的键值)。匿名组件不能被匹配。
最多可以缓存多少组件实例。一旦这个数字达到了,在新实例被创建之前,已缓存组件中最久没有被访问的实例会被销毁掉。
<keep-alive :max="10"> <component :is="view"></component> </keep-alive>
Props:
立即学习“前端免费学习笔记(深入)”;
name - string,用于具名插槽
Props:
立即学习“前端免费学习笔记(深入)”;
to - string。需要 prop,必须是有效的查询选择器或 HTMLElement (如果在浏览器环境中使用)。指定将在其中移动
<!-- 正确 --> <teleport to="#some-id" /> <teleport to=".some-class" /> <teleport to="[data-teleport]" /> <!-- 错误 --> <teleport to="h1" /> <teleport to="some-string" />
disabled - boolean。此可选属性可用于禁用
<teleport to="#popup" :disabled="displayVideoInline"> <video src="./my-movie.mp4"> </teleport>
请注意,这将移动实际的 DOM 节点,而不是被销毁和重新创建,并且它还将保持任何组件实例的活动状态。所有有状态的 HTML 元素 (即播放的视频) 都将保持其状态。
用于协调对组件树中嵌套的异步依赖的处理。
Props
interface SuspenseProps { timeout?: string | number }
事件
@resolve
@pending
@fallback
详细信息
如果在渲染时遇到异步依赖项 (异步组件和具有 async setup() 的组件),它将等到所有异步依赖项解析完成时再显示默认插槽。
以上就是vue内置组件有哪些的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号