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

vue3中Teleport的用法以及使用場(chǎng)景小結(jié)

 更新時(shí)間:2025年05月21日 15:05:36   作者:像素檢測(cè)儀  
Teleport是一個(gè)內(nèi)置組件,它可以將一個(gè)組件內(nèi)部的一部分模板傳送到該組件的?DOM?結(jié)構(gòu)外層的位置去,本文主要介紹了Vue3中Teleport用法以及使用場(chǎng)景小結(jié),感興趣的可以了解一下

1. 基本概念

Teleport 是 Vue3 提供的一個(gè)內(nèi)置組件,它可以將組件的內(nèi)容傳送到 DOM 樹的任何位置,而不受組件層級(jí)的限制。這在處理模態(tài)框、通知、彈出菜單等需要突破組件層級(jí)限制的場(chǎng)景中特別有用。

1.1 基本語法

<template>
  <teleport to="body">
    <!-- 這里的內(nèi)容會(huì)被傳送到 body 標(biāo)簽下 -->
    <div class="modal">
      <!-- 模態(tài)框內(nèi)容 -->
    </div>
  </teleport>
</template>

2. 常見使用場(chǎng)景

2.1 模態(tài)框

<!-- Modal.vue -->
<template>
  <teleport to="body">
    <div v-if="isOpen" class="modal-overlay">
      <div class="modal">
        <div class="modal-header">
          <h3>{{ title }}</h3>
          <button @click="close">×</button>
        </div>
        <div class="modal-body">
          <slot></slot>
        </div>
        <div class="modal-footer">
          <slot name="footer">
            <button @click="close">關(guān)閉</button>
          </slot>
        </div>
      </div>
    </div>
  </teleport>
</template>

<script setup>
const props = defineProps({
  isOpen: Boolean,
  title: String
})

const emit = defineEmits(['update:isOpen'])

const close = () => {
  emit('update:isOpen', false)
}
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal {
  background: white;
  padding: 20px;
  border-radius: 8px;
  min-width: 300px;
}
</style>

2.2 通知提示

<!-- Notification.vue -->
<template>
  <teleport to="#notifications-container">
    <div
      v-if="show"
      :class="['notification', type]"
      @click="close"
    >
      <div class="notification-content">
        {{ message }}
      </div>
    </div>
  </teleport>
</template>

<script setup>
import { ref, onMounted } from 'vue'

const props = defineProps({
  message: String,
  type: {
    type: String,
    default: 'info'
  },
  duration: {
    type: Number,
    default: 3000
  }
})

const show = ref(true)

const close = () => {
  show.value = false
}

onMounted(() => {
  if (props.duration > 0) {
    setTimeout(close, props.duration)
  }
})
</script>

<style scoped>
.notification {
  position: fixed;
  top: 16px;
  right: 16px;
  padding: 12px 24px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.info {
  background: #e6f7ff;
  border: 1px solid #91d5ff;
}

.success {
  background: #f6ffed;
  border: 1px solid #b7eb8f;
}

.error {
  background: #fff2f0;
  border: 1px solid #ffccc7;
}
</style>

2.3 上下文菜單

<!-- ContextMenu.vue -->
<template>
  <teleport to="body">
    <div
      v-if="show"
      class="context-menu"
      :style="position"
    >
      <slot></slot>
    </div>
  </teleport>
</template>

<script setup>
import { ref, computed } from 'vue'

const props = defineProps({
  show: Boolean,
  x: Number,
  y: Number
})

const position = computed(() => ({
  left: props.x + 'px',
  top: props.y + 'px'
}))
</script>

<style scoped>
.context-menu {
  position: fixed;
  background: white;
  border: 1px solid #eee;
  border-radius: 4px;
  padding: 8px 0;
  min-width: 160px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}
</style>

3. 高級(jí)用法

3.1 動(dòng)態(tài)目標(biāo)

<template>
  <teleport :to="target">
    <div class="content">
      動(dòng)態(tài)傳送的內(nèi)容
    </div>
  </teleport>
</template>

<script setup>
import { ref, onMounted } from 'vue'

const target = ref('body')

onMounted(() => {
  // 可以根據(jù)條件動(dòng)態(tài)改變目標(biāo)
  if (window.innerWidth < 768) {
    target.value = '#mobile-container'
  }
})
</script>

3.2 多個(gè) Teleport 到同一目標(biāo)

<template>
  <teleport to="#notifications">
    <div class="notification">通知 1</div>
  </teleport>
  
  <teleport to="#notifications">
    <div class="notification">通知 2</div>
  </teleport>
</template>

3.3 條件性傳送

<template>
  <teleport to="body" :disabled="isMobile">
    <div class="modal">
      <!-- 在移動(dòng)端不會(huì)被傳送,保持原位置 -->
    </div>
  </teleport>
</template>

<script setup>
import { ref, onMounted } from 'vue'

const isMobile = ref(false)

onMounted(() => {
  isMobile.value = window.innerWidth < 768
  
  window.addEventListener('resize', () => {
    isMobile.value = window.innerWidth < 768
  })
})
</script>

4. 實(shí)際應(yīng)用示例

4.1 全局加載指示器

<!-- LoadingIndicator.vue -->
<template>
  <teleport to="body">
    <div v-if="loading" class="loading-overlay">
      <div class="loading-spinner"></div>
      <div class="loading-text">{{ message }}</div>
    </div>
  </teleport>
</template>

<script setup>
defineProps({
  loading: Boolean,
  message: {
    type: String,
    default: '加載中...'
  }
})
</script>

<style scoped>
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(255, 255, 255, 0.9);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}
</style>

4.2 圖片預(yù)覽器

<!-- ImageViewer.vue -->
<template>
  <teleport to="body">
    <div
      v-if="visible"
      class="image-viewer"
      @click="close"
    >
      <img
        :src="imageUrl"
        @click.stop
      >
      <div class="controls">
        <button @click.stop="prev">&lt;</button>
        <button @click.stop="next">&gt;</button>
      </div>
    </div>
  </teleport>
</template>

<script setup>
const props = defineProps({
  visible: Boolean,
  imageUrl: String,
  images: Array
})

const emit = defineEmits(['update:visible'])

const close = () => {
  emit('update:visible', false)
}

const prev = () => {
  // 實(shí)現(xiàn)上一張邏輯
}

const next = () => {
  // 實(shí)現(xiàn)下一張邏輯
}
</script>

5. 最佳實(shí)踐

5.1 目標(biāo)元素管理

<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
  <title>Vue App</title>
</head>
<body>
  <div id="app"></div>
  
  <!-- 為 Teleport 預(yù)留的容器 -->
  <div id="modals"></div>
  <div id="notifications"></div>
  <div id="tooltips"></div>
</body>
</html>

5.2 組件封裝

<!-- BaseModal.vue -->
<template>
  <teleport to="#modals">
    <transition name="modal">
      <div
        v-if="modelValue"
        class="modal-container"
        @click.self="close"
      >
        <div class="modal-content">
          <slot></slot>
        </div>
      </div>
    </transition>
  </teleport>
</template>

<script setup>
defineProps({
  modelValue: Boolean
})

const emit = defineEmits(['update:modelValue'])

const close = () => {
  emit('update:modelValue', false)
}
</script>

6. 注意事項(xiàng)

  • 目標(biāo)元素存在性檢查
<template>
  <teleport to="#target" :disabled="!targetExists">
    <div>內(nèi)容</div>
  </teleport>
</template>

<script setup>
import { ref, onMounted } from 'vue'

const targetExists = ref(false)

onMounted(() => {
  targetExists.value = !!document.querySelector('#target')
})
</script>
  • SSR 兼容性
<template>
  <client-only>
    <teleport to="body">
      <div>僅客戶端渲染的內(nèi)容</div>
    </teleport>
  </client-only>
</template>
  • 清理工作
<script setup>
import { onUnmounted } from 'vue'

onUnmounted(() => {
  // 確保清理所有傳送的內(nèi)容
  const target = document.querySelector('#target')
  if (target) {
    target.innerHTML = ''
  }
})
</script>

到此這篇關(guān)于vue3中Teleport的用法以及使用場(chǎng)景小結(jié)的文章就介紹到這了,更多相關(guān)Vue3 Teleport內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • vue.js template模板的使用

    vue.js template模板的使用

    這篇文章主要介紹了vue.js template模板的使用,用到了4個(gè)組件,分別是header.vue,goods.vue,ratings.vue,seller.vue,感興趣的朋友跟隨腳本之家小編一起看看實(shí)現(xiàn)代碼
    2018-08-08
  • Vite 與 Webpack 開發(fā)/打包時(shí)環(huán)境變量對(duì)比分析

    Vite 與 Webpack 開發(fā)/打包時(shí)環(huán)境變量對(duì)比分析

    Webpack和Vite在環(huán)境變量的處理上有顯著區(qū)別,主要體現(xiàn)在變量前綴、訪問方式、內(nèi)置環(huán)境變量、配置文件使用、注入原理以及熱更新感知等方面,本文介紹Vite與Webpack開發(fā)/打包時(shí)環(huán)境變量對(duì)比分析,感興趣的朋友一起看看吧
    2026-04-04
  • Vue頁面返回滾動(dòng)位置恢復(fù)(keep-alive滾動(dòng)記憶)

    Vue頁面返回滾動(dòng)位置恢復(fù)(keep-alive滾動(dòng)記憶)

    Vue中通過自定義組合式函數(shù)useScroll,結(jié)合ref響應(yīng)式狀態(tài)、生命周期鉤子及DOM引用,實(shí)現(xiàn)keep-alive緩存組件的滾動(dòng)位置保存與恢復(fù),優(yōu)化用戶返回時(shí)的瀏覽體驗(yàn),感興趣的可以了解一下
    2025-09-09
  • 淺談Vue SSR 的 Cookies 問題

    淺談Vue SSR 的 Cookies 問題

    本篇文章主要介紹了淺談Vue SSR 的 Cookies 問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • 過濾器vue.filters的使用方法實(shí)現(xiàn)

    過濾器vue.filters的使用方法實(shí)現(xiàn)

    這篇文章主要介紹了過濾器vue.filters的使用方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • vue右鍵菜單的簡(jiǎn)單封裝

    vue右鍵菜單的簡(jiǎn)單封裝

    這篇文章主要為大家詳細(xì)介紹了vue右鍵菜單的簡(jiǎn)單封裝,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • vue實(shí)現(xiàn)拖拽效果

    vue實(shí)現(xiàn)拖拽效果

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)拖拽效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • vue上傳圖片組件編寫代碼

    vue上傳圖片組件編寫代碼

    這篇文章主要為大家詳細(xì)介紹了vue上傳圖片組件的編寫代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • vue實(shí)現(xiàn)員工信息錄入功能

    vue實(shí)現(xiàn)員工信息錄入功能

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)員工信息錄入功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • vue-element-admin下載到登錄的一些坑

    vue-element-admin下載到登錄的一些坑

    本文主要介紹了vue-element-admin下載到登錄的一些坑,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04

最新評(píng)論

出国| 北辰区| 抚顺市| 昆明市| 张家港市| 湖北省| 商南县| 米易县| 福州市| 芦溪县| 清远市| 大英县| 闽侯县| 吉木萨尔县| 德昌县| 洱源县| 三台县| 五大连池市| 寿宁县| 武威市| 横山县| 贵定县| 武鸣县| 奉新县| 永昌县| 包头市| 安阳市| 牙克石市| 宝兴县| 法库县| 四川省| 鸡泽县| 吴江市| 贺州市| 康平县| 霞浦县| 康定县| 甘洛县| 中卫市| 教育| 繁峙县|