我是靠谱客的博主 火星上小鸽子,这篇文章主要介绍Vue3内置组件Teleport使用方法详解,现在分享给大家,希望可以做个参考。

前言:

Vue 3.0 新增了一个内置组件 teleport 主要是为了解决以下场景:

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

场景举例:一个 Button ,点击后呼出模态对话框

这个模态对话框的业务逻辑位置肯定是属于这个 Button ,但是按照 DOM 结构来看,模态对话框的实际位置应该在整个应用的中间

这样就有了一个问题:组件的逻辑位置和DOM位置不在一起

按照以前 Vue2 的做法,一般是使用 position: fixed; 等CSS属性强行把对话框定位到了应用的中间位置,属于没有办法的办法,下面展示下 teleport 的基础用法。

1、Teleport用法

用法非常简单,只需要使用 to 这个属性就可以把组件渲染到想要的位置

复制代码
1
2
3
4
5
6
// 渲染到body标签下 <teleport to="body"> <div class="modal"> I'm a teleported modal! </div> </teleport>

也可以使用:

复制代码
1
2
3
<teleport to="#some-id" /> <teleport to=".some-class" /> <teleport to="[data-teleport]" />

必须是有效的查询选择器或 HTMLElement

进一步

2、完成模态对话框组件

现在我们来封装一个标准的模态对话框

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<template> <teleport to="body"> <transition name="dialog-fade"> <div class="dialog-wrapper" v-show="visible"> <div class="dialog"> <div class="dialog-header"> <slot name="title"> <span class="dialog-title"> {{ title }} </span> </slot> </div> <div class="dialog-body"> <slot></slot> </div> <div class="dialog-footer"> <slot name="footer"> <button @click="onClose">关闭</button> </slot> </div> </div> </div> </transition> </teleport> </template> <script> import { defineComponent } from 'vue'; export default defineComponent({ name: 'tdialog' }); </script> <script setup> const props = defineProps({ title: String, visible: Boolean }); const emit = defineEmits(['close']); const onClose = () => { emit('close'); }; </script>

使用的时候,只需要

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
<t-dialog title="LGD是不可战胜的" :visible="visible" @close="onClose"> 这是一段内容,萧瑟仙贝。 </t-dialog> // ... const visible = ref(false); const onDialog = () => { visible.value = !visible.value; }; const onClose = () => { visible.value = false; };

更进一步

3、组件的渲染

上面我们已经把标准的模态对话框组件完成了,还有另外一种相似的,只需要展示少量文字的轻量级提示组件 Message

在上面的例子中,我们总是把 TDialog 组件写在使用的地方,但是这个 Messgae 组件,我们在想提示的时候使用一个js语句就把它呼出来,类似于下面的这样

复制代码
1
2
// 呼出一个提示 Message({ message: '这是一条Message消息' });

想使用一个函数呼出来,我们需要准备下这个函数,这个函数的作用就是完成组件的渲染。

复制代码
1
2
3
4
5
6
7
8
const Message = options => { // 准备渲染容器 const container = document.createElement('div'); // 生成vnode const vnode = createVNode(MessageConstructor, options); // 渲染 render(vnode, container); };

MessageConstructor 是什么?就是我们的SFC(单文件组件):

复制代码
1
2
3
4
5
6
7
<template> <teleport to="#app"> <transition name="message-fade"> <div v-show="visible" ref="ins" class="message" :style="customStyle">{{ message }}</div> </transition> </teleport> </template>

在线 体验

查看 代码

总结:

以上就是关于 teleport 组件的基础用法和扩展用法,给我们提供了不少的方便,到此这篇关于Vue3内置组件Teleport使用方法详解的文章就介绍到这了,更多相关Vue3内置组件Teleport用法内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!

最后

以上就是火星上小鸽子最近收集整理的关于Vue3内置组件Teleport使用方法详解的全部内容,更多相关Vue3内置组件Teleport使用方法详解内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(136)

评论列表共有 0 条评论

立即
投稿
返回
顶部