選單
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Vue 教程

Vue 首頁 Vue Intro Vue Directives Vue v-bind Vue v-if Vue v-show Vue v-for Vue Events Vue v-on Vue Methods Vue Event Modifiers Vue Forms Vue v-model Vue CSS Binding Vue Computed Properties Vue Watchers Vue Templates

Scaling Up

Vue Why, How and Setup Vue First SFC Page Vue Components Vue Props Vue v-for Components Vue $emit() Vue Fallthrough Attributes Vue Scoped Styling Vue Local Components Vue Slots Vue v-slot Vue Scoped Slots Vue Dynamic Components Vue Teleport Vue HTTP Request Vue Template Refs Vue Lifecycle Hooks Vue Provide/Inject Vue Routing Vue Form Inputs Vue Animations Vue Animations with v-for Vue Build Vue Composition API

Vue Reference

Vue Built-in Attributes Vue Built-in Components Vue Built-in Elements Vue Component Instance Vue Directives Vue Instance Options Vue Lifecycle Hooks

Vue 示例

Vue Examples Vue Exercises Vue Quiz Vue Server Vue Certificate

Vue Teleport

Vue 的 <Teleport> 標籤用於將內容移動到 DOM 結構中的不同位置。

<Teleport> 和 'to' 屬性

要將某些內容移動到 DOM 結構中的其他位置,我們使用 Vue 的 <Teleport> 標籤包裹內容,並使用 'to' 屬性來定義移動到的位置。

<Teleport to="body">
  <p>Hello!</p>
</Teleport>

'to' 屬性的值以 CSS 符號表示法給出,因此,如果我們想將一些內容傳送到 body 標籤(如上面的程式碼所示),我們只需編寫 <Teleport to="body">

載入頁面後,我們可以透過檢查頁面來看到內容已被移至 body 標籤。

示例

CompOne.vue:

<template>
  <div>
    <h2>Component</h2>
    <p>This is the inside of the component.</p>
    <Teleport to="body">
      <div id="redDiv">Hello!</div>
    </Teleport>
  </div>
</template>
執行示例 »

如果我們右鍵單擊頁面並選擇“檢查”,我們可以看到紅色的 <div> 元素已被移出元件,並移到了 <body> 標籤的末尾。

我們也可以例如有一個帶有 id <div id="receivingDiv"> 的標籤,然後透過使用 <Teleport to="#receivingDiv"> 包裹我們想要傳送/移動的內容,將一些內容傳送/移動到該 <div> 中。


被傳送元素的指令碼和樣式

即使某些內容被 <Teleport> 標籤移出了元件,<script><style> 標籤中與被移動內容相關的程式碼仍然可以正常工作。

示例

儘管被傳送的 <div> 元素在編譯後不再位於元件內,但來自 <style><script> 標籤的相關程式碼仍然適用於該 <div> 標籤。

CompOne.vue:

<template>
  <div>
    <h2>Component</h2>
    <p>This is the inside of the component.</p>
    <Teleport to="body">
      <div 
        id="redDiv" 
        @click="toggleVal = !toggleVal" 
        :style="{ backgroundColor: bgColor }"
      >
        Hello!<br>
        Click me!
      </div>
    </Teleport>
  </div>
</template>

<script>
export default {
  data() {
    return {
      toggleVal: true
    }
  },
  computed: {
    bgColor() {
      if (this.toggleVal) {
        return 'lightpink'
      }
      else {
        return 'lightgreen'
      }
    }
  }
}
</script>

<style scoped>
#redDiv {
  margin: 10px;
  padding: 10px;
  display: inline-block;
}

#redDiv:hover {
  cursor: pointer;
}
</style>
執行示例 »


×

聯絡銷售

如果您想將 W3Schools 服務用於教育機構、團隊或企業,請傳送電子郵件給我們
sales@w3schools.com

報告錯誤

如果您想報告錯誤,或想提出建議,請傳送電子郵件給我們
help@w3schools.com

W3Schools 經過最佳化,旨在方便學習和培訓。示例可能經過簡化,以提高閱讀和學習體驗。教程、參考資料和示例會不斷審查,以避免錯誤,但我們無法保證所有內容的完全正確性。使用 W3Schools 即表示您已閱讀並接受我們的使用條款Cookie 和隱私政策

版權所有 1999-2024 Refsnes Data。保留所有權利。W3Schools 由 W3.CSS 提供支援