自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Vue3值得注意的新特性之——teleport

開發(fā) 前端
Vue鼓勵我們通過將UI和相關行為封裝到組件中來構建UI。我們可以將它們嵌套在另一個內(nèi)部,來構建一個組成應用程序UI樹。

前提

Vue鼓勵我們通過將UI和相關行為封裝到組件中來構建UI。我們可以將它們嵌套在另一個內(nèi)部,來構建一個組成應用程序UI樹。

然而,有時組件模板的一部分邏輯上屬于該組件,而從技術角度來看,最好將模板的這一部分移動到DOM中Vue app之外的其他位置。

Teleport提供了一種干凈的方法,允許我們控制在DOM中哪個父節(jié)點下渲染HTML,而不必求助于全局狀態(tài)或將其拆分為兩個組件。

  1. app.component('modal-button', { 
  2.   template: ` 
  3.     <button @click="modalOpen = true"
  4.         Open full screen modal! (With teleport!) 
  5.     </button> 
  6.  
  7.     <teleport to="body"
  8.       <div v-if="modalOpen" class="modal"
  9.         <div> 
  10.           I'm a teleported modal!  
  11.           (My parent is "body"
  12.           <button @click="modalOpen = false"
  13.             Close 
  14.           </button> 
  15.         </div> 
  16.       </div> 
  17.     </teleport> 
  18.   `, 
  19.   data() { 
  20.     return {  
  21.       modalOpen: false 
  22.     } 
  23.   } 
  24. }) 

使用

與Vue compoents一起使用

如果<teleport>包含Vue組件,則它仍將是<teleport>父組件的邏輯子組件:

  1. const app = Vue.createApp({ 
  2.   template: ` 
  3.     <h1>Root instance</h1> 
  4.     <parent-component /> 
  5.   ` 
  6. }) 
  7.  
  8. app.component('parent-component', { 
  9.   template: ` 
  10.     <h2>This is a parent component</h2> 
  11.     <teleport to="#endofbody"
  12.       <child-component name="John" /> 
  13.     </teleport> 
  14.   ` 
  15. }) 
  16.  
  17. app.component('child-component', { 
  18.   props: ['name'], 
  19.   template: ` 
  20.     <div>Hello, {{ name }}</div> 
  21.   ` 
  22. }) 

在這種情況下,即使在不同地方渲染child-compoents,它仍將是parent-component的子集,并將從中接受name prop。

這也意味著來自父組件的注入按預期工作,并且子組件將嵌套在Vue Devtools中的父組件之下,部署放在實際內(nèi)容移動到的位置。

在同一目標上使用多個teleport

一個常見的用例場景是一個可重用的<Modal>組件,他可能同時有多個實例處于活動狀態(tài)。對于這種情況,多個<teleport>組件可以將其內(nèi)容掛載到同一個目標元素。順序將是一個簡單的追加——稍后掛載將位于目標元素中較早的掛載之后。

  1. <teleport to="#modals"
  2.   <div>A</div> 
  3. </teleport> 
  4. <teleport to="#modals"
  5.   <div>B</div> 
  6. </teleport> 
  7.  
  8. <!-- result--> 
  9. <div id="modals"
  10.   <div>A</div> 
  11.   <div>B</div> 
  12. </div> 

使用

to:String。需要prop,必須是有效的查詢選擇器或HTMLElement(如果在瀏覽器環(huán)境中使用)。指定將在其移動<teleport>內(nèi)容的目標元素。

  1. <!-- 正確 --> 
  2. <teleport to="#some-id" /> 
  3. <teleport to=".some-class" /> 
  4. <teleport to="[data-teleport]" /> 
  5.  
  6. <!-- 錯誤 --> 
  7. <teleport to="h1" /> 
  8. <teleport to="some-string" /> 

disabled: boolean。此可選屬性可用于禁用<teleport>的功能,這意味著其插槽內(nèi)容不會移動到任何位置。而是在您在周圍父組件中指定了<teleport>的位置渲染。

  1. <teleport to="#popup" :disabled="displayVideoInline"
  2.   <video src="./my-movie.mp4"
  3. </teleport> 

值得注意的是,這將移動實際的DOM節(jié)點,而不是被銷毀和重新創(chuàng)建,而且它還將保持

任何組件實例的活動狀態(tài)。所有有狀態(tài)的HTML元素(即播放的視頻)都將保持其狀態(tài)。

 

責任編輯:張燕妮 來源: 今日頭條
相關推薦

2021-05-12 10:25:53

組件驗證漏洞

2015-06-23 15:48:41

Swift 2.0iOS9

2020-12-01 08:34:31

Vue3組件實踐

2023-11-29 09:05:59

Vue 3場景

2010-07-21 16:28:33

職場

2022-07-14 08:22:48

Computedvue3

2021-03-31 08:01:50

Vue3 Vue2 Vue3 Telepo

2013-09-16 13:18:28

遺留系統(tǒng)系統(tǒng)遷移

2010-11-26 15:05:58

MySQL變量

2021-12-31 07:48:58

Vue3 插件Vue應用

2024-10-24 09:18:45

2011-05-12 09:29:54

2017-02-21 13:20:02

SD-WAN軟件定義網(wǎng)絡廣域網(wǎng)

2009-06-15 14:53:00

NetBeans 6.

2010-03-31 15:52:24

Oracle子查詢

2015-10-08 09:25:05

比特幣存儲開源

2021-08-11 08:31:42

前端技術Vue3

2023-03-15 18:25:25

2010-07-12 10:48:21

SQL Server數(shù)

2025-04-21 00:05:00

點贊
收藏

51CTO技術棧公眾號