vue2實現(xiàn)傳送門效果的示例
描述
在Vue3中<Teleport> 是一個內(nèi)置組件,使我們可以將一個組件的一部分模板“傳送”到該組件的 DOM 層次結(jié)構(gòu)之外的 DOM 節(jié)點中;現(xiàn)在在vue2實現(xiàn)一下效果:將組件掛載到body上
先看效果


代碼
Teleport 組件:
<script>
export default {
props: {
to: {
type: String,
required: true
},
disabled: {
type: Boolean,
required: true
}
},
inject: ['parent'],
// inject 選項應(yīng)該是:
// 一個字符串?dāng)?shù)組,或 一個對象,對象的 key 是本地的綁定名,value 是:
// 在可用的注入內(nèi)容中搜索用的 key (字符串或 Symbol),或
// 一個對象,該對象的:
// from property 是在可用的注入內(nèi)容中搜索用的 key (字符串或 Symbol)
// default property 是降級情況下使用的 value
// Vue 選項中的 render 函數(shù)若存在,則 Vue 構(gòu)造函數(shù)不會從 template 選項或通過 el 選項指定的掛載元素中提取出的 HTML 模板編譯渲染函數(shù)。
render() {
return <div class="Teleport">{this.$scopedSlots.default()}</div>
// $scopedSlots用來訪問作用域插槽。對于包括 默認(rèn) slot 在內(nèi)的每一個插槽,該對象都包含一個返回相應(yīng) VNode 的函數(shù)。
},
watch: {
disabled() {
if (!this.disabled) {
// this指向組件的實例。$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 組件掛載時機(jī)
isTeleport: false, // 控制什么時候被傳送
};
},
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實現(xiàn)傳送門效果的示例的文章就介紹到這了,更多相關(guān)vue2 傳送門內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue實現(xiàn)導(dǎo)航欄點擊當(dāng)前標(biāo)簽變色功能
這篇文章主要為大家詳細(xì)介紹了Vue實現(xiàn)導(dǎo)航欄點擊當(dāng)前標(biāo)簽變色功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05
使用vite搭建ssr活動頁架構(gòu)的實現(xiàn)
本文主要介紹了使用vite搭建ssr活動頁架構(gòu),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
vue項目中應(yīng)用ueditor自定義上傳按鈕功能
這篇文章主要介紹了vue項目中應(yīng)用ueditor自定義上傳按鈕功能,文中以vue-cli生成的項目為例給大家介紹了vue項目中使用ueditor的方法,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-04-04
vue組件中iview的modal組件爬坑問題之modal的顯示與否應(yīng)該是使用v-show
這篇文章主要介紹了vue組件中iview的modal組件爬坑問題之modal的顯示與否應(yīng)該是使用v-show,本文通過實例圖文相結(jié)合的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04
vue?watch監(jiān)聽觸發(fā)優(yōu)化搜索框的性能防抖節(jié)流的比較
這篇文章主要為大家介紹了vue?watch監(jiān)聽觸發(fā)優(yōu)化搜索框的性能防抖節(jié)流的比較,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10

