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

很多人不知道可以使用這種 Key 的方式來對 Vue 組件進行重新渲染!

開發(fā) 前端
在某些情況下,我們必須強制Vue重新渲染組件,如果沒有,那可能,你做的業(yè)務(wù)還不夠負責(zé),反正我是經(jīng)常需要重新渲染組件,哈哈。

 在某些情況下,我們必須強制Vue重新渲染組件,如果沒有,那可能,你做的業(yè)務(wù)還不夠負責(zé),反正我是經(jīng)常需要重新渲染組件,哈哈。

[[332579]]

雖然Vue不會自動更新這種情況是相對比較少,但是知道如何在出現(xiàn)這個問題時修復(fù)它還是很有用的。

在大多數(shù)情況下,此問題根源還是我們對 Vue 的響應(yīng)式理解還是不夠到位。因此,要盡量確保我們要正確使用了Vue。響應(yīng)式有時過于棘手,我也經(jīng)常不知道所措。

這節(jié),我們就來做一些之前很少做過或者沒做過的:用 key 來讓組件重新渲染。

在這篇文章中,會涉及到這幾個知識點:

  • key 是如何改變組件
  • key 如何與多個子組件一起工作
  • 如何強制子組件自己更新

通過改變 key 的值來重新渲染組件

我最喜歡的方法是使用key屬性,因為使用key 的方式,Vue 就知道了特定組件與特定數(shù)據(jù)相關(guān)。

如果 key保持不變,則不會更改組件。但是,如果key發(fā)生更改, Vue 知道它應(yīng)該刪除舊組件并創(chuàng)建一個新組件。

下面是一個非常基本的方法:

  1. <template> 
  2.   <ComponentToReRender 
  3.     :key="componentKey" 
  4.   /> 
  5. </template> 
  6.  
  7. <script> 
  8.   export default { 
  9.     data() { 
  10.       return { 
  11.         componentKey: 0, 
  12.       }; 
  13.     }, 
  14.     methods: { 
  15.       forceRerender() { 
  16.         this.componentKey += 1; 
  17.       } 
  18.     } 
  19.   } 
  20. </script> 

每次調(diào)用forceRerender時,componentKey 的值就會更改。當(dāng)componentKey 的值發(fā)生改變時,Vue 就知道把ComponentToReRender組件刪除并創(chuàng)建一個新組件。

這樣ComponentToReRender就會重新渲染并重置里面的狀態(tài)。nice nice!

強制多個子節(jié)點進行更新

同樣用這種方式也可以用于多個子組件:

  1. <template> 
  2.   <div> 
  3.     <Child 
  4.       :key="key1" 
  5.     /> 
  6.     <Child 
  7.       :key="key2" 
  8.     /> 
  9.   </div> 
  10. </template> 
  11.  
  12. <script> 
  13.   export default { 
  14.     data() { 
  15.       return { 
  16.         key1: 0, 
  17.         key2: 0, 
  18.       }; 
  19.     }, 
  20.     methods: { 
  21.       forceRerender(child) { 
  22.         if (child === 1) { 
  23.           this.key1 += 1; 
  24.         } else if( child === 2) { 
  25.           this.key2 += 1; 
  26.         } 
  27.       } 
  28.     } 
  29.   } 
  30. </script> 

這里我們使用了兩個單獨 key 來分別控制每個子組件是否重新渲染。將它們分開是為了其中的一個子組件渲染,不會影響到另外另一個。

但如果希望兩個子組件總是一起更新,則可以使用相同的 kye。但是,key必須是唯一的,所以下面這種方式,不能工作:

  1. <template> 
  2.   <div> 
  3.     <Child 
  4.       :key="componentKey" 
  5.     /> 
  6.     <Child 
  7.       :key="componentKey" 
  8.     /> 
  9.   </div> 
  10. </template> 
  11.  
  12. <script> 
  13.   export default { 
  14.     data() { 
  15.       return { 
  16.         componentKey: 0, 
  17.       }; 
  18.     }, 
  19.     methods: { 
  20.       forceRerender(child) { 
  21.         this.componentKey += 1; 
  22.       } 
  23.     } 
  24.   } 
  25. </script> 

在這里,僅第一個Child組件會被渲染。第二個被忽略,因為它具有重復(fù)的key 了。

為了解決這個問題,我們可以基于componentKey為每個孩子構(gòu)造一個新key:

  1. <template> 
  2.   <div> 
  3.     <Child 
  4.       :key="`${componentKey}-1`" 
  5.     /> 
  6.     <Child 
  7.       :key="`${componentKey}-2`" 
  8.     /> 
  9.   </div> 
  10. </template> 
  11.  
  12. <script> 
  13.   export default { 
  14.     data() { 
  15.       return { 
  16.         componentKey: 0, 
  17.       }; 
  18.     }, 
  19.     methods: { 
  20.       forceRerender(child) { 
  21.         this.componentKey += 1; 
  22.       } 
  23.     } 
  24.   } 
  25. </script> 

因為我們每次在componentKey后面添加-1和-2,所以這兩個key始終是唯一的,現(xiàn)在這兩個組件都將被重新渲染。

如果是在列表中,則可以使用如下方式:

  1. <template> 
  2.   <div> 
  3.     <Child 
  4.       v-for="(item, index) in list" 
  5.       :key="`${componentKey}-${index}`" 
  6.     /> 
  7.   </div> 
  8. </template> 
  9.  
  10. <script> 
  11.   export default { 
  12.     data() { 
  13.       return { 
  14.         list: [ 
  15.           // ... 
  16.         ], 
  17.         componentKey: 0, 
  18.       }; 
  19.     }, 
  20.     methods: { 
  21.       forceRerender(child) { 
  22.         this.componentKey += 1; 
  23.       } 
  24.     } 
  25.   } 
  26. </script> 

在這里,我們將key構(gòu)造為${componentKey}-${index},因此列表中的每個項目都會獲得唯一的key,只要componentKey一改變,列表中的所有組件將同時重新渲染。

當(dāng)然,還有更簡單的方式,就是用div把列表包裹起來,直接對 div重新更新就行了:

  1. <template> 
  2.   <div :key="componentKey"
  3.     <Child 
  4.       v-for="item in list" 
  5.       :key="item.id" 
  6.     /> 
  7.   </div> 
  8. </template> 
  9.  
  10. <script> 
  11.   export default { 
  12.     data() { 
  13.       return { 
  14.         list: [ 
  15.           // ... 
  16.         ], 
  17.         componentKey: 0, 
  18.       }; 
  19.     }, 
  20.     methods: { 
  21.       forceRerender(child) { 
  22.         this.componentKey += 1; 
  23.       } 
  24.     } 
  25.   } 
  26. </script> 

這中思路可以用在很多地方,可以為我們擺脫很的困境,大家要牢記起來。

好了,今天就跟大家分享到這里,我們下期在見,謝謝大家的觀看。

作者:Michael Thiessen 譯者:前端小智 來源:medium

原文:https://morioh.com/p/08963bf07353

本文轉(zhuǎn)載自微信公眾號「大遷世界」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系大遷世界公眾號。

 

責(zé)任編輯:武曉燕 來源: 大遷世界
相關(guān)推薦

2020-06-29 08:28:36

v-for 解構(gòu)函數(shù)

2020-07-14 08:43:54

VueHTML函數(shù)

2021-01-15 05:39:13

HashMapHashTableTreeMap

2015-07-22 11:53:29

云計算AWS分析癱瘓

2020-07-01 08:36:43

CSS規(guī)范web

2021-08-24 00:13:23

Windows 10Windows微軟

2022-12-05 15:23:33

JavaScript技巧運算符

2019-01-07 09:27:39

2021-08-27 10:03:12

人工智能AI

2020-11-20 06:13:04

Like %

2018-08-10 10:36:25

SSL證書誤區(qū)

2019-12-13 19:52:29

人工智能AI

2021-08-08 21:53:40

Arthas指令表達式

2023-06-05 08:07:34

聚集索引存儲數(shù)據(jù)

2024-09-12 08:32:42

2022-07-06 10:33:39

技術(shù)債務(wù)CIO

2021-01-12 12:33:20

Pandas技巧代碼

2021-11-02 19:14:58

Spring數(shù)據(jù)

2021-01-07 05:27:20

包導(dǎo)入變量

2021-05-08 23:19:25

微信存儲小程序
點贊
收藏

51CTO技術(shù)棧公眾號