Vue3內(nèi)置組件Teleport使用方法詳解
前言:
Vue 3.0
新增了一個內(nèi)置組件 teleport
,主要是為了解決以下場景:
有時組件模板的一部分邏輯上屬于該組件,而從技術(shù)角度來看,最好將模板的這一部分移動到 DOM
中 Vue app
之外的其他位置
場景舉例:一個 Button
,點擊后呼出模態(tài)對話框
這個模態(tài)對話框的業(yè)務(wù)邏輯位置肯定是屬于這個 Button
,但是按照 DOM
結(jié)構(gòu)來看,模態(tài)對話框的實際位置應(yīng)該在整個應(yīng)用的中間
這樣就有了一個問題:組件的邏輯位置和DOM
位置不在一起
按照以前 Vue2
的做法,一般是使用 position: fixed
; 等CSS屬性強行把對話框定位到了應(yīng)用的中間位置,屬于沒有辦法的辦法,下面展示下 teleport
的基礎(chǔ)用法。
1、Teleport用法
用法非常簡單,只需要使用 to 這個屬性就可以把組件渲染到想要的位置
// 渲染到body標(biāo)簽下 <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
進一步
2、完成模態(tài)對話框組件
現(xiàn)在我們來封裝一個標(biāo)準(zhǔn)的模態(tài)對話框
<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">關(guān)閉</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是不可戰(zhàn)勝的" :visible="visible" @close="onClose"> 這是一段內(nèi)容,蕭瑟仙貝。 </t-dialog> // ... const visible = ref(false); const onDialog = () => { visible.value = !visible.value; }; const onClose = () => { visible.value = false; };
更進一步
3、組件的渲染
上面我們已經(jīng)把標(biāo)準(zhǔn)的模態(tài)對話框組件完成了,還有另外一種相似的,只需要展示少量文字的輕量級提示組件 Message
在上面的例子中,我們總是把 TDialog
組件寫在使用的地方,但是這個 Messgae
組件,我們在想提示的時候使用一個js語句就把它呼出來,類似于下面的這樣
// 呼出一個提示 Message({ message: '這是一條Message消息' });
想使用一個函數(shù)呼出來,我們需要準(zhǔn)備下這個函數(shù),這個函數(shù)的作用就是完成組件的渲染。
const Message = options => { // 準(zhǔn)備渲染容器 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" :style="customStyle">{{ message }}</div> </transition> </teleport> </template>
在線 體驗
查看 代碼
總結(jié):
以上就是關(guān)于 teleport
組件的基礎(chǔ)用法和擴展用法,給我們提供了不少的方便,到此這篇關(guān)于Vue3內(nèi)置組件Teleport使用方法詳解的文章就介紹到這了,更多相關(guān)Vue3內(nèi)置組件Teleport用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決vue單頁路由跳轉(zhuǎn)后scrollTop的問題
今天小編就為大家分享一篇解決vue單頁路由跳轉(zhuǎn)后scrollTop的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09vue3中使用props和emits并指定其類型與默認(rèn)值
props是Vue3中的一個重要概念,它允許我們將數(shù)據(jù)從父組件傳遞到子組件,下面這篇文章主要給大家介紹了關(guān)于vue3中使用props和emits并指定其類型與默認(rèn)值的相關(guān)資料,需要的朋友可以參考下2023-04-04Vue3中級指南之如何在vite中使用svg圖標(biāo)詳解
在以webpack為構(gòu)建工具的開發(fā)環(huán)境中我們可以很方便的實現(xiàn)SVG圖標(biāo)的組件化,下面這篇文章主要給大家介紹了關(guān)于Vue3中級指南之如何在vite中使用svg圖標(biāo)的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04vue的index.html中獲取環(huán)境變量和業(yè)務(wù)判斷圖文詳解
這篇文章主要給大家介紹了關(guān)于vue的index.html中獲取環(huán)境變量和業(yè)務(wù)判斷的相關(guān)資料,對vue來說index.html是一個總的入口文件,vue是單頁面應(yīng)用,掛在id為app的div下然后動態(tài)渲染路由模板,需要的朋友可以參考下2023-09-09