最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue2如何實現(xiàn)vue3的teleport

 更新時間:2022年04月18日 15:48:14   作者:最有才的河南大漢  
這篇文章主要介紹了vue2如何實現(xiàn)vue3的teleport,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

vue2實現(xiàn)vue3的teleport

不支持同一目標上使用多個teleport(代碼通過v-if就能實現(xiàn))

組件

<script>
?? ?export default {
?? ??? ?name: 'teleport',
?? ??? ?props: {
?? ??? ??? ?/* 移動至哪個標簽內(nèi),最好使用id */
?? ??? ??? ?to: {
?? ??? ??? ??? ?type: String,
?? ??? ??? ??? ?required: true
?? ??? ??? ?}
?? ??? ?},

?? ??? ?mounted() {
?? ??? ??? ?document.querySelector(this.to).appendChild(this.$el)
?? ??? ?},

?? ??? ?destroyed() {
?? ??? ??? ?document.querySelector(this.to).removeChild(this.$el)
?? ??? ?},

?? ??? ?render() {
?? ??? ??? ?return <div>{this.$scopedSlots.default()}</div>
?? ??? ?}
?? ?}
</script>

使用

<teleport to="#header__left">
?? ?<div>
?? ??? ?當前組件引用{{msg}}
?? ?</div>
</teleport>

vue3新特性teleport介紹

teleport是什么

Teleport 是一種能夠?qū)⑽覀兊哪0逡苿拥?DOM 中 Vue app 之外的其他位置的技術(shù)。

如果我們嵌套在 Vue 的某個組件內(nèi)部,那么處理嵌套組件的定位、z-index 和樣式就會變得很困難。

使用Teleport 就可以方便的解決組件間 css 層級問題

teleport怎么使用

要使用teleport,首先要在頁面上添加一個元素,我們要將模態(tài)內(nèi)容移動到該頁面

下面舉個例子

// index.html
<body>
? ...
? <div id="app"></div><!--Vue mounting element-->
? <div id="modal-wrapper">
? ? <!--modal should get moved here-->
? </div>
</body>

我們將模態(tài)內(nèi)容包裝在 teleport 組件中,還需要指定一個 to 屬性,為該屬性分配一個查詢選擇器,以標識目標元素,在本例中為 #modal-wrapper

// App.vue
<template>
? <button @click="toggleModalState">Open modal</button>
? <teleport to="#modal-wrapper">
? ? <modal v-if="modalOpen">
? ? ? <p>Hello, I'm a modal window.</p>
? ? </modal>
? </teleport>
</template>

teleport 中的任何內(nèi)容都將渲染在目標元素中

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

南丹县| 威远县| 邮箱| 沙雅县| 竹山县| 成安县| 海丰县| 横峰县| 斗六市| 涡阳县| 剑川县| 正宁县| 阳泉市| 保亭| 曲水县| 山阳县| 江达县| 桃源县| 珠海市| 华安县| 西华县| 浙江省| 南康市| 通化县| 昌宁县| 报价| 六盘水市| 九寨沟县| 日照市| 新竹县| 耒阳市| 赫章县| 崇礼县| 延吉市| 合江县| 陈巴尔虎旗| 扎囊县| 吴桥县| 武陟县| 闽侯县| 宾川县|