这篇文章主要讲解了“如何使用Vue3内置组件Teleport”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何使用Vue3内置组件Teleport”吧!
1、Teleport用法
2、完成模态对话框组件
3、组件的渲染
前言:
Vue 3.0
新增了一个内置组件 teleport
,主要是为了解决以下场景:
有时组件模板的一部分逻辑上属于该组件,而从技术角度来看,最好将模板的这一部分移动到 DOM
中 Vue app
之外的其他位置
场景举例:一个 Button
,点击后呼出模态对话框
这个模态对话框的业务逻辑位置肯定是属于这个 Button
,但是按照 DOM
结构来看,模态对话框的实际位置应该在整个应用的中间
这样就有了一个问题:组件的逻辑位置和DOM
位置不在一起
按照以前 Vue2
的做法,一般是使用 position: fixed
; 等CSS属性强行把对话框定位到了应用的中间位置,属于没有办法的办法,下面展示下 teleport
的基础用法。
用法非常简单,只需要使用 to 这个属性就可以把组件渲染到想要的位置
// 渲染到body标签下
<teleport to="body">
<div class="modal">
I'm a teleported modal!
</div>
</teleport>
也可以使用:
<teleport to="#some-id" />
<teleport to=".some-class" />
<teleport to="[data-teleport]" />
必须是有效的查询选择器或 HTMLElement
进一步
现在我们来封装一个标准的模态对话框
<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>
使用的时候,只需要
<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;
};
更进一步
上面我们已经把标准的模态对话框组件完成了,还有另外一种相似的,只需要展示少量文字的轻量级提示组件 Message
在上面的例子中,我们总是把 TDialog
组件写在使用的地方,但是这个 Messgae
组件,我们在想提示的时候使用一个js语句就把它呼出来,类似于下面的这样
// 呼出一个提示
Message({ message: '这是一条Message消息' });
想使用一个函数呼出来,我们需要准备下这个函数,这个函数的作用就是完成组件的渲染。
const Message = options => {
// 准备渲染容器
const container = document.createElement('div');
// 生成vnode
const vnode = createVNode(MessageConstructor, options);
// 渲染
render(vnode, container);
};
MessageConstructor
是什么?就是我们的SFC(单文件组件):
<template>
<teleport to="#app">
<transition name="message-fade">
<div v-show="visible" ref="ins" class="message" :>{{ message }}</div>
</transition>
</teleport>
</template>
在线 体验
查看 代码
感谢各位的阅读,以上就是“如何使用Vue3内置组件Teleport”的内容了,经过本文的学习后,相信大家对如何使用Vue3内置组件Teleport这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。