就目前所瞭解的狀況,key的做用有如下這些。html
場景一大同小異司空見慣,場景二是下面這樣的:vue
<div :key="rerender"> <span>Hello Vue.js !</span> <complexComponent :propObj="propObj" :propArr="propArr" ></complexComponent> </div> refresh(){ this.rerender = + new Date(); }
那麼vue中key的相關知識點究竟是怎樣的呢?node
<ul> <li v-for="item in items" :key="item.id">...</li> </ul>
<transition> <span :key="text">{{ text }}</span> </transition>
text發生變化時,<span>
會被replaced,而不會patched,所以transition會被觸發。
個人理解:
text變化時,span的key發生了變化,也就是說曾經擁有了舊key的span再也不出現了,當擁有新值的text做爲key時,擁有了新key的span出現了,那麼舊key span會被移除,舊transition也會移除,新key span觸發渲染,新transition觸發。git
結合官方API的知識點,如今再來回顧文章開頭提出的場景。github
答案:算法
<div :key="rerender"> <span>Hello Vue.js !</span> <complexComponent :propObj="propObj" :propArr="propArr" ></complexComponent> </div> refresh(){ this.rerender = + new Date(); }
答案:api
思考:數組
因爲Vue.js的obj和arr存在沒法檢測到數據變化的狀況,obj是屬性的新增和刪除(緣由是新增和刪除都沒有觸發setter,watcher未告訴外界更新),arr則是數組內元素從新賦值或者修改length屬性(緣由是沒有使用改變數組自己的方法,沒有觸發數組原型鏈攔截器,watcher未告訴外界更新)。
因此!經過賦予新key的方式,移除舊key div,渲染新key div,propObj和propArr在complexComponent組件內會從新觸發一次生命週期,作一次從新渲染。此時父組件的propObj和propArr js變量其實已經獲取到新值了,只是沒有觸發DOM也好,VNode也好的從新渲染。須要經過刷新key去force update,說到forceUpdate,能夠經過$forceUpdate()去手動強制更新DOM。ide
場景:父組件修改傳遞給子組件的數據,數組數據的更新沒有按照this.$set去更新。該怎麼辦?ui
this.productImages.forEach((product) => { if (product.productId in this.productsState) { product.status = this.productsState[product.productId]; } });
不使用this.$set去賦值數據的不能rerender的緣由是什麼?
在Vue.js中,對Array的變化偵測是經過攔截原型的方式實現的。也就經過對push,pop,shift,unshift,splice,sort,reverse,fill,copyWithin去改變數組自身內容的方法作攔截,從而響應。而product.status = this.productsState[product.productId];
沒有觸發任何改變數組自身的被監聽的方法,所以不會rerender。
加在this.productImages的父元素上就好。
若不涉及數據傳遞,也能夠直接加在須要更新的element上。
如今是粗暴的+new Date()時間戳作key值的。
也能夠用雙向綁定的值做爲key值,保證新舊key值不一樣就行。
vue.js的虛擬DOM算法,在更新vNode時,須要從舊vNode列表中查找與新vNode節點相同的vNode進行更新,若是這個過程設置了屬性key,過程就會快不少。
其餘具體見上文。
只能在父組件調用這個方法,手動通知vue實例從新渲染。
// $forceUpdate源碼 Vue.prototype.$forceUpdate = function () { const vm: Component = this if (vm._watcher) { vm._watcher.update() } } // update源碼 /** * Subscriber interface. * Will be called when a dependency changes. */ update () { /* istanbul ignore else */ if (this.lazy) { this.dirty = true } else if (this.sync) { this.run() } else { queueWatcher(this) } }
product.status = this.productsState[product.productId];
之後,其實此時dep已經發生變化了,可是Vue.js數組響應式的實現因爲是攔截原型鏈方法的方式,沒有檢測到這個變化,因此不會自動rerender,沒有觸發update。所以咱們經過$forceUpdate的方式,調用包含dep的watcher上的update方法,從而作到rerender。
不能夠。
由於dep是父組件的watcher和dep,並非子組件,是父組件的this.productImages沒有被檢測到並實時更新,並非子組件的問題。
https://vuejs.org/v2/api/#key
https://vuejs.org/v2/api/#vm-...
https://vuejs.org/v2/guide/co...