vue2實(shí)現(xiàn)傳送門效果的示例
描述
在Vue3中<Teleport>
是一個(gè)內(nèi)置組件,使我們可以將一個(gè)組件的一部分模板“傳送”到該組件的 DOM 層次結(jié)構(gòu)之外的 DOM 節(jié)點(diǎn)中;現(xiàn)在在vue2實(shí)現(xiàn)一下效果:將組件掛載到body上
先看效果
代碼
Teleport 組件:
<script> export default { props: { to: { type: String, required: true }, disabled: { type: Boolean, required: true } }, inject: ['parent'], // inject 選項(xiàng)應(yīng)該是: // 一個(gè)字符串?dāng)?shù)組,或 一個(gè)對(duì)象,對(duì)象的 key 是本地的綁定名,value 是: // 在可用的注入內(nèi)容中搜索用的 key (字符串或 Symbol),或 // 一個(gè)對(duì)象,該對(duì)象的: // from property 是在可用的注入內(nèi)容中搜索用的 key (字符串或 Symbol) // default property 是降級(jí)情況下使用的 value // Vue 選項(xiàng)中的 render 函數(shù)若存在,則 Vue 構(gòu)造函數(shù)不會(huì)從 template 選項(xiàng)或通過 el 選項(xiàng)指定的掛載元素中提取出的 HTML 模板編譯渲染函數(shù)。 render() { return <div class="Teleport">{this.$scopedSlots.default()}</div> // $scopedSlots用來訪問作用域插槽。對(duì)于包括 默認(rèn) slot 在內(nèi)的每一個(gè)插槽,該對(duì)象都包含一個(gè)返回相應(yīng) VNode 的函數(shù)。 }, watch: { disabled() { if (!this.disabled) { // this指向組件的實(shí)例。$el指向當(dāng)前組件的DOM元素。 document.querySelector(this.to).appendChild(this.$el); } else { this.parent.toSourceDom(this.$el); } } }, mounted() { if(!this.disabled) document.querySelector(this.to).appendChild(this.$el) }, methods: {}, }; </script> <style lang="scss" scoped> .Teleport { width: 100%; height: 100%; } </style>
index.vue 中引用 Teleport.vue 組件
<template> <div> <Teleport v-if="isShow" :disabled="isTeleport" to="body"> <div class="cover"> <div class="inner"> 我是彈窗 <div class="close" @click="closed">關(guān)閉按鈕</div> </div> </div> </Teleport> <button @click="show">顯示</button> </div> </template> <script> import Teleport from "./Teleport.vue"; import model from "./model.vue"; export default { data() { return { isShow: false, // 控制 Teleport 組件掛載時(shí)機(jī) isTeleport: false, // 控制什么時(shí)候被傳送 }; }, provide() { return { parent: this, }; }, components: { Teleport, model }, methods: { show() { this.isShow = true; }, closeModel() { this.isShow = false; }, toSourceDom(dom) { document.getElementById("sourceBox").appendChild(dom); }, }, }; </script> <style lang="scss" scoped> .cover { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba($color: #000000, $alpha: 0.3); .inner { width: 500px; height: 300px; border-radius: 10px; background: #fff; color: red; font-weight: 600; position: absolute; left: 40%; top: 30%; text-align: center; font-size: 30px; .close{ position: absolute; bottom: 0; right: 0; background: skyblue; padding: 10px; cursor: pointer; border-radius: 10px 0 0 0; } } } </style>
到此這篇關(guān)于vue2實(shí)現(xiàn)傳送門效果的示例的文章就介紹到這了,更多相關(guān)vue2 傳送門內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
el-popover如何通過js手動(dòng)控制彈出框顯示、隱藏
最近項(xiàng)目中多次用到了Popover彈出框,下面這篇文章主要給大家介紹了關(guān)于el-popover如何通過js手動(dòng)控制彈出框顯示、隱藏的相關(guān)資料,需要的朋友可以參考下2023-12-12Vue實(shí)現(xiàn)導(dǎo)航欄點(diǎn)擊當(dāng)前標(biāo)簽變色功能
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)導(dǎo)航欄點(diǎn)擊當(dāng)前標(biāo)簽變色功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05使用vite搭建ssr活動(dòng)頁架構(gòu)的實(shí)現(xiàn)
本文主要介紹了使用vite搭建ssr活動(dòng)頁架構(gòu),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07vue項(xiàng)目中應(yīng)用ueditor自定義上傳按鈕功能
這篇文章主要介紹了vue項(xiàng)目中應(yīng)用ueditor自定義上傳按鈕功能,文中以vue-cli生成的項(xiàng)目為例給大家介紹了vue項(xiàng)目中使用ueditor的方法,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-04-04vue項(xiàng)目中實(shí)現(xiàn)的微信分享功能示例
這篇文章主要介紹了vue項(xiàng)目中實(shí)現(xiàn)的微信分享功能,結(jié)合實(shí)例形式分析了基于vue.js實(shí)現(xiàn)的微信分享功能具體定義與使用方法,需要的朋友可以參考下2019-01-01vue組件中iview的modal組件爬坑問題之modal的顯示與否應(yīng)該是使用v-show
這篇文章主要介紹了vue組件中iview的modal組件爬坑問題之modal的顯示與否應(yīng)該是使用v-show,本文通過實(shí)例圖文相結(jié)合的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04vue?watch監(jiān)聽觸發(fā)優(yōu)化搜索框的性能防抖節(jié)流的比較
這篇文章主要為大家介紹了vue?watch監(jiān)聽觸發(fā)優(yōu)化搜索框的性能防抖節(jié)流的比較,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10