Vue 3.0 内置组件

#component

  • Props:

  • is - string | Component

  • 用法:

渲染一个“元组件”为动态组件。依 is 的值,来决定哪个组件被渲染。is 的值是一个字符串,它既可以是 HTML 标签名称也可以是组件名称。

 
  1. <!-- 动态组件由 vm 实例的 `componentId` property 控制 -->
  2. <component :is="componentId"></component>
  3. <!-- 也能够渲染注册过的组件或 prop 传入的组件-->
  4. <component :is="$options.components.child"></component>
  5. <!-- 可以通过字符串引用组件 -->
  6. <component :is="condition ? 'FooComponent' : 'BarComponent'"></component>
  7. <!-- 可以用来渲染原生 HTML 元素 -->
  8. <component :is="href ? 'a' : 'span'"></component>

#transition

  • Props:

  • name - string 用于自动生成 CSS 过渡类名。例如:name: 'fade' 将自动拓展为 .fade-enter.fade-enter-active 等。
  • appear - boolean,是否在初始渲染时使用过渡。默认为 false
  • persisted - boolean。如果是 true,表示这是一个不真实插入/删除元素的转换,而是切换显示/隐藏状态。过渡钩子被注入,但渲染器将跳过。相反,自定义指令可以通过调用注入的钩子 (例如 v-show) 来控制转换。
  • 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 - string
  • appear-class - string
  • enter-to-class - string
  • leave-to-class - string
  • appear-to-class - string
  • enter-active-class - string
  • leave-active-class - string
  • appear-active-class - string

  • 事件:

  • before-enter
  • before-leave
  • enter
  • leave
  • appear
  • after-enter
  • after-leave
  • after-appear
  • enter-cancelled
  • leave-cancelled (仅 v-show)
  • appear-cancelled

  • 用法:

<transition> 元素作为单个元素/组件的过渡效果。<transition> 只会把过渡效果应用到其包裹的内容上,而不会额外渲染 DOM 元素,也不会出现在可被检查的组件层级中。

 
  1. <!-- 单个元素 -->
  2. <transition>
  3. <div v-if="ok">toggled content</div>
  4. </transition>
  5. <!-- 动态组件 -->
  6. <transition name="fade" mode="out-in" appear>
  7. <component :is="view"></component>
  8. </transition>
  9. <!-- 事件钩子 -->
  10. <div id="transition-demo">
  11. <transition @after-enter="transitionComplete">
  12. <div v-show="ok">toggled content</div>
  13. </transition>
  14. </div>

 
  1. const app = Vue.createApp({
  2. ...
  3. methods: {
  4. transitionComplete (el) {
  5. // 因为传递了'el'的DOM元素作为参数
  6. }
  7. }
  8. ...
  9. })
  10. app.mount('#transition-demo')

#transition-group

  • Props:

  • tag - string,默认为 span
  • move-class - 覆盖移动过渡期间应用的 CSS 类。
  • 除了 mode,其他 attribute 和 <transition> 相同。

  • 事件:

  • 事件和 <transition> 相同。

  • 用法:

<transition-group> 元素作为多个元素/组件的过渡效果。<transition-group> 渲染一个真实的 DOM 元素。默认渲染 <span>,可以通过 tag attribute 配置哪个元素应该被渲染。

注意,每个 <transition-group> 的子节点必须有独立的 key,动画才能正常工作

<transition-group> 支持通过 CSS transform 过渡移动。当一个子节点被更新,从屏幕上的位置发生变化,它会被应用一个移动中的 CSS 类 (通过 name attribute 或配置 move-class attribute 自动生成)。如果 CSS transform property 是“可过渡”property,当应用移动类时,将会使用 FLIP 技术使元素流畅地到达动画终点。

 
  1. <transition-group tag="ul" name="slide">
  2. <li v-for="item in items" :key="item.id">
  3. {{ item.text }}
  4. </li>
  5. </transition-group>

#keep-alive

  • Props:

  • include - string | RegExp | Array。只有名称匹配的组件会被缓存。
  • exclude - string | RegExp | Array。任何名称匹配的组件都不会被缓存。
  • max - number | string。最多可以缓存多少组件实例。

  • 用法:

<keep-alive> 包裹动态组件时,会缓存不活动的组件实例,而不是销毁它们。和 <transition> 相似,<keep-alive> 是一个抽象组件:它自身不会渲染一个 DOM 元素,也不会出现在组件的父组件链中。

当组件在 <keep-alive> 内被切换,它的 activated 和 deactivated 这两个生命周期钩子函数将会被对应执行。

主要用于保留组件状态或避免重新渲染。

 
  1. <!-- 基本 -->
  2. <keep-alive>
  3. <component :is="view"></component>
  4. </keep-alive>
  5. <!-- 多个条件判断的子组件 -->
  6. <keep-alive>
  7. <comp-a v-if="a > 1"></comp-a>
  8. <comp-b v-else></comp-b>
  9. </keep-alive>
  10. <!-- 和 `<transition>` 一起使用 -->
  11. <transition>
  12. <keep-alive>
  13. <component :is="view"></component>
  14. </keep-alive>
  15. </transition>

注意,<keep-alive> 是用在其一个直属的子组件被切换的情形。如果你在其中有 v-for 则不会工作。如果有上述的多个条件性的子元素,<keep-alive> 要求同时只有一个子元素被渲染。

  • include 和 exclude

The include 和 exclude prop 允许组件有条件地缓存。二者都可以用逗号分隔字符串、正则表达式或一个数组来表示:

 
  1. <!-- 逗号分隔字符串 -->
  2. <keep-alive include="a,b">
  3. <component :is="view"></component>
  4. </keep-alive>
  5. <!-- regex (使用 `v-bind`) -->
  6. <keep-alive :include="/a|b/">
  7. <component :is="view"></component>
  8. </keep-alive>
  9. <!-- Array (使用 `v-bind`) -->
  10. <keep-alive :include="['a', 'b']">
  11. <component :is="view"></component>
  12. </keep-alive>

匹配首先检查组件自身的 name 选项,如果 name 选项不可用,则匹配它的局部注册名称 (父组件 components 选项的键值)。匿名组件不能被匹配。

  • max

最多可以缓存多少组件实例。一旦这个数字达到了,在新实例被创建之前,已缓存组件中最久没有被访问的实例会被销毁掉。

 
  1. <keep-alive :max="10">
  2. <component :is="view"></component>
  3. </keep-alive>

WARNING

<keep-alive> 不会在函数式组件中正常工作,因为它们没有缓存实例。

#slot

  • Props:

  • name - string,用于具名插槽

  • 用法:

<slot> 元素作为组件模板之中的内容分发插槽。<slot> 元素自身将被替换。

详细用法,请参考下面教程的链接。

#teleport

  • Props:

  • to - string。需要 prop,必须是有效的查询选择器或 HTMLElement (如果在浏览器环境中使用)。指定将在其中移动 <teleport> 内容的目标元素

 
  1. <!-- 正确 -->
  2. <teleport to="#some-id" />
  3. <teleport to=".some-class" />
  4. <teleport to="[data-teleport]" />
  5. <!-- 错误 -->
  6. <teleport to="h1" />
  7. <teleport to="some-string" />

  • disabled - boolean。此可选属性可用于禁用 <teleport> 的功能,这意味着其插槽内容将不会移动到任何位置,而是在您在周围父组件中指定了 <teleport> 的位置渲染。

 
  1. <teleport to="#popup" :disabled="displayVideoInline">
  2. <video src="./my-movie.mp4">
  3. </teleport>

请注意,这将移动实际的 DOM 节点,而不是被销毁和重新创建,并且它还将保持任何组件实例的活动状态。所有有状态的 HTML 元素 (即播放的视频) 都将保持其状态。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3.0中的组件高级功能包括保持动态组件状态和引用DOM元素。 保持动态组件状态的方法是使用Vue内置的<keep-alive>组件。通过将动态组件包裹在<keep-alive>标签中,可以在切换动态组件时保持组件的状态。例如: ```html <keep-alive> <component :is="comName"></component> </keep-alive> ``` 在这个例子中,comName是一个data属性,用于指定当前要渲染的组件的名称。通过在<component>标签中使用:is属性,可以动态地指定要渲染的组件。 引用DOM元素的方法是使用ref属性。通过给DOM元素添加ref属性,可以在组件中通过$refs属性获取对DOM元素的引用。例如: ```html <template> <h3>MyRef组件</h3> <button @click="getRef">获取$refs引用</button> </template> <script> export default { methods: { getRef() { console.log(this.$refs) // $refs指向一个空对象,需要在DOM元素上添加ref属性 } } } </script> ``` 在这个例子中,点击按钮后,调用getRef方法可以在控制台上输出当前组件实例对象this。通过this.$refs可以访问到DOM元素的引用。 以上就是Vue3.0组件高级功能的两个示例,分别是保持动态组件状态和引用DOM元素。希望能对你有所帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Vue3.0----组件高级【下】(第五章)](https://blog.csdn.net/QQ675396947/article/details/127486948)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值