偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

很多人不知道可以使用這種 Key 的方式來(lái)對(duì) Vue 組件進(jìn)行重新渲染!

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

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

[[332579]]

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

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

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

在這篇文章中,會(huì)涉及到這幾個(gè)知識(shí)點(diǎn):

  • key 是如何改變組件
  • key 如何與多個(gè)子組件一起工作
  • 如何強(qiáng)制子組件自己更新

通過(guò)改變 key 的值來(lái)重新渲染組件

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

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

下面是一個(gè)非常基本的方法:

  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時(shí),componentKey 的值就會(huì)更改。當(dāng)componentKey 的值發(fā)生改變時(shí),Vue 就知道把ComponentToReRender組件刪除并創(chuàng)建一個(gè)新組件。

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

強(qiáng)制多個(gè)子節(jié)點(diǎn)進(jìn)行更新

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

  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> 

這里我們使用了兩個(gè)單獨(dú) key 來(lái)分別控制每個(gè)子組件是否重新渲染。將它們分開(kāi)是為了其中的一個(gè)子組件渲染,不會(huì)影響到另外另一個(gè)。

但如果希望兩個(gè)子組件總是一起更新,則可以使用相同的 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> 

在這里,僅第一個(gè)Child組件會(huì)被渲染。第二個(gè)被忽略,因?yàn)樗哂兄貜?fù)的key 了。

為了解決這個(gè)問(wèn)題,我們可以基于componentKey為每個(gè)孩子構(gòu)造一個(gè)新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> 

因?yàn)槲覀兠看卧赾omponentKey后面添加-1和-2,所以這兩個(gè)key始終是唯一的,現(xiàn)在這兩個(gè)組件都將被重新渲染。

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

  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},因此列表中的每個(gè)項(xiàng)目都會(huì)獲得唯一的key,只要componentKey一改變,列表中的所有組件將同時(shí)重新渲染。

當(dāng)然,還有更簡(jiǎn)單的方式,就是用div把列表包裹起來(lái),直接對(duì) 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> 

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

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

作者:Michael Thiessen 譯者:前端小智 來(lái)源:medium

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

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

 

責(zé)任編輯:武曉燕 來(lái)源: 大遷世界
相關(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

云計(jì)算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技巧運(yùn)算符

2019-01-07 09:27:39

2020-11-20 06:13:04

Like %

2021-08-27 10:03:12

人工智能AI

2018-08-10 10:36:25

SSL證書(shū)誤區(qū)

2019-12-13 19:52:29

人工智能AI

2021-08-08 21:53:40

Arthas指令表達(dá)式

2023-06-05 08:07:34

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

2021-01-12 12:33:20

Pandas技巧代碼

2024-09-12 08:32:42

2021-11-02 19:14:58

Spring數(shù)據(jù)

2025-05-29 01:55:00

Vue3.5API性能

2025-04-16 07:06:43

2022-07-06 10:33:39

技術(shù)債務(wù)CIO
點(diǎn)贊
收藏

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