Vue 3.0 Teleport 【Vue3 从零开始】

Vue 鼓励我们通过将 UI 和相关行为封装到组件中来构建 UI。我们可以将它们嵌套在另一个内部,以构建一个组成应用程序 UI 的树。

然而,有时组件模板的一部分逻辑上属于该组件,而从技术角度来看,最好将模板的这一部分移动到 DOM 中 Vue app 之外的其他位置。

一个常见的场景是创建一个包含全屏模式的组件。在大多数情况下,你希望模态框的逻辑存在于组件中,但是模态框的快速定位就很难通过 CSS 来解决,或者需要更改组件组合。

考虑下面的 HTML 结构。

 
  1. <body>
  2. <div style="position: relative;">
  3. <h3>Tooltips with Vue 3 Teleport</h3>
  4. <div>
  5. <modal-button></modal-button>
  6. </div>
  7. </div>
  8. </body>

让我们来看看 modal-button 组件:

该组件将有一个 button 元素来触发模态的打开,以及一个具有类 .modal 的 div 元素,它将包含模态的内容和一个用于自关闭的按钮。

 
  1. const app = Vue.createApp({});
  2. app.component('modal-button', {
  3. template: `
  4. <button @click="modalOpen = true">
  5. Open full screen modal!
  6. </button>
  7. <div v-if="modalOpen" class="modal">
  8. <div>
  9. I'm a modal!
  10. <button @click="modalOpen = false">
  11. Close
  12. </button>
  13. </div>
  14. </div>
  15. `,
  16. data() {
  17. return {
  18. modalOpen: false
  19. }
  20. }
  21. })

当在初始的 HTML 结构中使用这个组件时,我们可以看到一个问题——模态是在深度嵌套的 div 中渲染的,而模态的 position:absolute 以父级相对定位的 div 作为引用。

Teleport 提供了一种干净的方法,允许我们控制在 DOM 中哪个父节点下呈现 HTML,而不必求助于全局状态或将其拆分为两个组件。

让我们修改 modal-button 以使用 <teleport>,并告诉 Vue “Teleport 这个 HTML 该‘body’标签”。

 
  1. app.component('modal-button', {
  2. template: `
  3. <button @click="modalOpen = true">
  4. Open full screen modal! (With teleport!)
  5. </button>
  6. <teleport to="body">
  7. <div v-if="modalOpen" class="modal">
  8. <div>
  9. I'm a teleported modal!
  10. (My parent is "body")
  11. <button @click="modalOpen = false">
  12. Close
  13. </button>
  14. </div>
  15. </div>
  16. </teleport>
  17. `,
  18. data() {
  19. return {
  20. modalOpen: false
  21. }
  22. }
  23. })

因此,一旦我们单击按钮打开模式,Vue 将正确地将模态内容渲染为 body 标签的子级。

点击此处实现

#与 Vue components 一起使用

如果 <teleport> 包含 Vue 组件,则它仍将是 <teleport> 父组件的逻辑子组件:

 
  1. const app = Vue.createApp({
  2. template: `
  3. <h1>Root instance</h1>
  4. <parent-component />
  5. `
  6. })
  7. app.component('parent-component', {
  8. template: `
  9. <h2>This is a parent component</h2>
  10. <teleport to="#endofbody">
  11. <child-component name="John" />
  12. </teleport>
  13. `
  14. })
  15. app.component('child-component', {
  16. props: ['name'],
  17. template: `
  18. <div>Hello, {{ name }}</div>
  19. `
  20. })

在这种情况下,即使在不同的地方渲染 child-component,它仍将是 parent-component 的子级,并将从中接收 name prop。

这也意味着来自父组件的注入按预期工作,并且子组件将嵌套在 Vue Devtools 中的父组件之下,而不是放在实际内容移动到的位置。

#在同一目标上使用多个 teleport

一个常见的用例场景是一个可重用的 <Modal> 组件,它可能同时有多个实例处于活动状态。对于这种情况,多个 <teleport> 组件可以将其内容挂载到同一个目标元素。顺序将是一个简单的追加——稍后挂载将位于目标元素中较早的挂载之后。

 
  1. <teleport to="#modals">
  2. <div>A</div>
  3. </teleport>
  4. <teleport to="#modals">
  5. <div>B</div>
  6. </teleport>
  7. <!-- result-->
  8. <div id="modals">
  9. <div>A</div>
  10. <div>B</div>
  11. </div>

你可以在 API 参考 查看 teleport 组件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值