当前位置: 首页 > news >正文

Vue3:<Teleport>传送门组件的使用和注意事项

你好,我是沐爸,欢迎点赞、收藏、评论和关注。

Vue3 引入了一个新的内置组件 <Teleport>,它允许你将子组件树渲染到 DOM 中的另一个位置,而不是在父组件的模板中直接渲染。这对于需要跳出当前组件的 DOM 层级结构进行渲染的场景非常有用,比如模态框(Modal)、下拉菜单(Dropdown)或者工具提示(Tooltip)等组件,这些组件通常需要在页面上的特定位置显示,而不是它们被声明的地方。

一、基本用法

<template><div><!-- 默认传送到 body 中 --><Teleport><p>This will be rendered in the body element.</p></Teleport><!-- 指定传送到特定的 DOM 元素 --><Teleport to="#custom-element"><p>This will be rendered in the #custom-element.</p></Teleport></div>
</template><script>
export default {// ...
};
</script><style>
/* 样式 */
</style>

在这个示例中,第一个 <Teleport> 组件将 <p> 元素默认传送到 body 元素中。第二个 <Teleport> 组件将 <p> 元素传送到 ID 为 custom-element 的 DOM 元素中。

二、指定传送目标

你可以使用 `to` 属性来指定传送的目标元素。如果 `to` 属性是一个字符串,它将被视为 CSS 选择器来查找目标元素。你也可以直接传递一个 DOM 元素作为 `to` 属性的值。
<template><div><Teleport :to="targetElement"><p>This will be rendered in the target element.</p></Teleport></div>
</template><script>
export default {data() {return {targetElement: null};},mounted() {this.targetElement = document.querySelector('#custom-element');}
};
</script>

在这个示例中,<Teleport> 组件将 <p> 元素传送到 mounted 钩子中指定的 targetElement 中。

三、动态传送目标

你可以动态地改变 `to` 属性的值,以在不同目标之间切换传送的内容。
<template><div><button @click="changeTarget">Change Target</button><Teleport :to="target"><p>This will be rendered in the target element.</p></Teleport></div>
</template><script>
export default {data() {return {target: 'body'};},methods: {changeTarget() {this.target = this.target === 'body' ? '#custom-element' : 'body';}}
};
</script>

在这个示例中,点击按钮会切换 <Teleport> 组件的目标,将 <p> 元素在 body#custom-element 之间传送。

四、搭配组件使用

`` 只改变了渲染的 DOM 结构,它不会影响组件间的逻辑关系。也就是说,如果 ``包含了一个组件,那么该组件始终和这个使用了``的组件保持逻辑上的父子关系。传入的 props 和触发的事件也会照常工作。

这也意味着来自父组件的注入也会按预期工作,子组件将在 Vue Devetools 中嵌套在父组件下面,而不是放在实际内容移动到的地方。

五、禁用 Teleport

在某些场景下可能需要视情况禁用 ``。举例来说,我们想要在桌面端将一个组件当做浮层来渲染,但在移动端则当作行内组件。我们可以通过对 ``动态传入一个 `disabled`prop 来处理这两种不同情况。
<Teleport :disabled="isMobile">...
</Teleport>

这里的 isMobile的值为 true 时,<Teleport>会被禁用。

注意,**<Teleport>**被禁用后,**<Teleport>****组件中的内容将作为当前组件的一部分被渲染,而不再被传送。**举例来看,假如有以下代码:

index.html

<div id="app"></div>
<div id="modals"><div>模态框</div>
</div>

App.vue

<template><div class="parent"><h3>父组件</h3><child-component></child-component></div> 
</template>

ChildComponent.vue

<template><div class="child"><h3>子组件</h3></div><Teleport to="#modals"><div>A</div></Teleport><Teleport to="#modals"><div>B</div></Teleport>
</template>

Teleport组件未禁用之前的效果如下:

修改Teleport组件,将其禁用

ChildComponent.vue

<template><div class="child"><h3>子组件</h3></div><Teleport to="#modals" :disabled="true"><div>A</div></Teleport><Teleport to="#modals"><div>B</div></Teleport>
</template>

效果如下:

六、多个 Teleport 共享目标

一个可重用的模态框组件可能同时存在多个实例。对于此类场景,多个 `Teleport`组件可以将其内容挂载到同一个目标元素上,而顺序就是简单的顺次追加,后挂载的将排在目标元素下更后面的位置上。

比如下面这样的用例:

<Teleport to="#modals"<div>A</div>
</Teleport><Teleport to="#modals"><div>B</div>
</Teleport>

渲染的结果为:

<div id="modals><div>A</div><div>B</div>
</div>

七、注意事项

当使用 `` 时,请确保目标 DOM 元素(即 `to` 属性指定的元素)在组件渲染之前就已经存在于页面上。如果目标元素不存在,Vue 将不会渲染 `` 的内容,同时控制台会报警告。

警告如下:

[Vue warn]: Failed to locate Teleport target with selector “#modals”. Note the target element must exist before the component is mounted - i.e. the target cannot be rendered by the component itself, and ideally should be outside of the entire Vue component tree.

还是以模态框为例,代码如下:

App.vue

<template><div class="parent"><h3>父组件</h3><child-component></child-component></div> 
</template>

ChildComponent.vue

<template><div class="child"><h3>子组件</h3></div><Teleport to="#modals"><div>A</div></Teleport>
</template>

效果如下:

解决方案是,确保这个元素在 Teleport组件被渲染之前存在于 DOM 中。如果是在单页面应用(SPA)中,可能需要在 Vue 实例挂载之前确保该元素存在。例如,在使用 Vue CLI 创建的项目中,你可以在 index.html 文件中添加如下代码来确保 “#modals” 容器存在:

index.html

<div id="app"></div>
<div id="modals"><div>模态框</div>
</div>

<Teleport> 组件是 Vue 3 中一个非常强大的功能,它提供了一种灵活的方式来控制组件的渲染位置,使得组件的布局更加灵活和动态。

好了,分享结束,谢谢点赞,下期再见。

http://www.lryc.cn/news/433764.html

相关文章:

  • 项目之家:又一家项目信息发布合作对接及一手接单平台
  • 02-java实习工作一个多月-经历分享
  • JVM 调优篇2 jvm的内存结构以及堆栈参数设置与查看
  • 微信可以设置自动回复吗?
  • 同样数据源走RTMP播放延迟低还是RTSP低?
  • @开发者极客们,网易2024低代码大赛来啦
  • 数据分析-16-时间序列分析的常用模型
  • SpringMVC使用:类型转换数据格式化数据验证
  • 多语言ASO – 本地化的10个技巧
  • C程序设计——函数0
  • 第二十一章 rust与动静态库的结合使用
  • 修改服务器DNS解析及修改自动对时时区
  • 中科院TOP“灌水神刊”合集!盘点那些“又牛又水”的国人友好SCI
  • Python列表浅拷贝的陷阱与破解之道
  • 开放式系统互连(OSI)模型的实际意义
  • 回溯——10.全排列 II
  • 基于百度AIStudio飞桨paddleRS-develop版道路模型开发训练
  • 【 C++ 】C/C++内存管理
  • 智能客服的演变:从传统到向量数据库的新时代
  • python使用超级鹰识别验证码
  • 基于YOLO目标检测实现表情识别(结合计算机视觉与深度学习的创新应用)
  • Keil导入包出错
  • 超声波自动气象站
  • Mysql事件操作
  • Python必知必会:程序员必须知道的22个Python单行代码!
  • MongoDB 的适用场景
  • 汽车EDI:montaplast EDI对接
  • 【idea】设置文件模板
  • 时间戳和日期相互转换+检验日期合法性功能C语言
  • SPIRNGBOOT+VUE实现浏览器播放音频流并合成音频